2017-10-06 49 views
0

我有一个属性配置Bean看起来像:春季启动文件的属性被忽略和CLASSPATH性质采取而是当值加载到XML配置

<bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
    <property name="ignoreUnresolvablePlaceholders" value="false"/> 
    <property name="ignoreResourceNotFound" value="true"/> 
    <property name="localOverride" value="false"/> 
    <property name="locations"> 
     <list> 
      <value>file://${emulator.config}</value> 
      <value>classpath:emulator.properties</value> 
      <value>file://${db.config}</value> 
      <value>classpath:db.properties</value> 
     </list> 
    </property> 
</bean> 

从第一个仿真器配置的值取在与@Value代码喜欢:

@Value("${emulator.database.template.create}") 
private String createDBPath; 

所有它们正确地采取所需的顺序:

  1. 如果我公司供应的外部文件路径,这样

    java \ 
    -Dlog4j.configurationFile=/correctfullpath/log4j2.xml \ 
    -Dspring.config.location=/correctfullpath/application.properties \ 
    -Demulator.config=/correctfullpath/emulator.properties \ 
    -Ddb.config=/correctfullpath/db.properties -jar target/registrator-emulator.war 
    

JVM选项我装从提供/correctfullpath/emulator.properties采取@超值装饰变量的值;

  • 如果JVM选项中省略,该值是从类路径属性文件取出罐子
  • 内和某事absolutelly不同,如果我获得的属性值于XML的可配置豆:

    <bean id="manageDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource"> 
        <property name="driverClassName" value="${database.driver}"/> 
        <property name="url" value="${database.url}"/> 
        <property name="username" value="${database.user}"/> 
        <property name="password" value="${database.password}"/> 
    </bean> 
    

    这里$ {database.url}使用从db.properties被忽略与-Ddb.config =/correctfullpath/db.properties选项提供的值取;它们总是从jar中的classpath属性文件中获取。唯一的解决方法(不是很适合我)是注释掉的classpath财产属性占位豆:

    <bean class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer"> 
        <property name="ignoreUnresolvablePlaceholders" value="false"/> 
        <property name="ignoreResourceNotFound" value="true"/> 
        <property name="localOverride" value="false"/> 
        <property name="locations"> 
         <list> 
          <value>file://${emulator.config}</value> 
          <value>classpath:emulator.properties</value> 
          <value>file://${db.config}</value> 
          <!--<value>classpath:db.properties</value>--> 
         </list> 
        </property> 
    </bean> 
    

    那么,有没有选项,强制在文件属性提供的classpath属性是XML配置忽略?

    UPD。根据M. Deinum的建议,我尝试了使用Spring引导注释配置的toscwitch。

    但我还是要注释掉的classpath db.properties:

    @Configuration 
    @PropertySources({@PropertySource("file://${emulator.config}"), 
         @PropertySource("classpath:emulator.properties"), 
         @PropertySource(value = "file://${statsd.config}",ignoreResourceNotFound = true), 
         @PropertySource("classpath:statsd.properties"), 
         @PropertySource(value = "file://${pools.config}",ignoreResourceNotFound = true), 
         @PropertySource("classpath:pools.properties"), 
         @PropertySource(value= "file://${db.config}"), 
         //@PropertySource("classpath:db.properties") 
         }) 
    

    在这里我如何创建bean:

    @Value( “$ {} database.driver”) private String dataBaseDriver;

    @Value("${database.url}") 
    private String dataBaseUrl; 
    
    @Value("${database.user}") 
    private String dataBaseUser; 
    
    @Value("${database.password}") 
    private String dataBasePassword; 
    
    
    
    
    @Bean 
    @Primary 
    public DataSource manageDataSource() { 
        return DataSourceBuilder 
          .create() 
          .username(dataBaseUser) 
          .password(dataBasePassword) 
          .url(dataBaseUrl) 
          .driverClassName(dataBaseDriver) 
          .build(); 
    } 
    
    +1

    您使用Spring引导你为什么要配置自己的'PropertySourcesPlaceholderConfigurer'?不要因为这会干扰Spring Boot已经添加的那个。相反,请在你的'@ Configuration'或者'@ SpringBootApplication'类中加入一个或多个'@ PropertySource'注解来加载其他文件。 –

    +0

    似乎是某种神奇的东西,但是切换到Springboot贬低配置没有任何改变:我仍然需要将属性注释到classpaht: –

    +0

    @PropertySources({@ PropertySource(“file:// $ {emulator.config} “), @PropertySource(”classpath:emulator.properties“), @PropertySource(value =”file:// $ {statsd.config}“,ignoreResourceNotFound = true), @PropertySource(”classpath:statsd.properties “), @PropertySource(value =”file:// $ {pools.config}“,ignoreResourceNotFound = true), @PropertySource(”classpath:pools.properties“), @PropertySource(value =”file:/ /${db.config}“), //@PropertySource("classpath:db.properties”) }) –

    回答

    0

    问题的根源在于属性顺序!

    由于文件属性覆盖类路径属性,它们应该放在classpath中的人后:

    @PropertySources({ 
         @PropertySource("classpath:emulator.properties"), 
         @PropertySource(value = "file://${emulator.config}", ignoreResourceNotFound = true), 
         @PropertySource("classpath:statsd.properties"), 
         @PropertySource(value = "file://${statsd.config}",ignoreResourceNotFound = true), 
         @PropertySource("classpath:pools.properties"), 
         @PropertySource(value = "file://${pools.config}",ignoreResourceNotFound = true), 
         @PropertySource("classpath:db.properties"), 
         @PropertySource(value= "file://${db.config}",ignoreResourceNotFound = true) 
         })