我有一个属性配置bean,如:
<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;
所有这些都按照所需顺序正确拍摄:
如果我提供像
这样的jvm选项的外部文件路径 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
我从加载的/correctfullpath/emulator.properties获取了加载到@ Value-decorated变量的值;
如果我正在为xml-configurated bean获取属性值,那么事情会变得完全不同:
<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属性文件中获取。唯一的解决方法(不太适合我)是从属性占位符bean注释掉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>
当提供文件属性时,是否有任何选项可以强制在xml配置中忽略类路径属性?
UPD。根据M. Deinum的建议,我尝试使用toscwitch进行Spring引导注释配置。
但我还是要注释掉类路径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(&#34; $ {database.driver}&#34) 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();
}
答案 0 :(得分:0)
问题的根源是属性顺序!
由于文件属性覆盖了类路径属性,因此应将它们放在类路径之后:
@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)
})