我敢肯定這已被問了1000次,因爲我看過他們,但是我錯過了一些東西。Spring @Value沒有設置
上下文:
<beans profile="localDev">
<util:properties id="propertiesLocalDev"location="classpath:/localDev.properties"/>
</beans>
<beans profile="test">
<util:properties id="properties-test" location="classpath:/test.properties"/>
</beans>
初始化:
System.setProperty("spring.profiles.active", "localDev");
GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
ctx.load("classpath:applicationContext.xml");
ctx.refresh();
配置:
@Configuration
public class AppConfig {
@Value("${test.value}")
private String testValue;
...
日誌:
INFO: Loading properties file from class path resource [localDev.properties]
個
屬性:
test.value=ugh
如此看來性能越來越讀,但是不獲取設置AppConfig.testValue值。我嘗試過使用純java java/xml等等...有些配置會破壞一些工作,嘗試使用@PropertySource,但常量是testValue永遠不會被設置,所以我從根本上做錯了什麼。
總體目標是根據不同的配置文件加載不同的屬性文件。
任何人都可以看到我做錯了什麼? 謝謝
我的理解是, 取代屬性佔位符。 –
Sector7B
util:屬性只是創建一個屬性bean。要解析其他bean中的屬性引用,您仍然需要一個propertyplaceholder。 –
剛剛嘗試過,它現在只是讀取屬性文件兩次,但仍然不起作用。 – Sector7B