我有一個屬性配置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;
所有它們正確地採取所需的順序:
如果我公司供應的外部文件路徑,這樣
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();
}
您使用Spring引導你爲什麼要配置自己的'PropertySourcesPlaceholderConfigurer'?不要因爲這會干擾Spring Boot已經添加的那個。相反,請在你的'@ Configuration'或者'@ SpringBootApplication'類中加入一個或多個'@ PropertySource'註解來加載其他文件。 –
似乎是某種神奇的東西,但是切換到Springboot貶低配置沒有任何改變:我仍然需要將屬性註釋到classpaht: –
@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」) }) –