场景:spring
两个配置文件:db.properties,application.properties数据库
在数据库配置里面引用db.propertiesapp
<bean id="propertyPlaceholderConfigurer" class="...">
<property name="locations">
<list>
<value>classpath*:/db.properties</value>
<value>file:/etc/db.properties</value>
</list>
</property>
</bean>
这时候,application.properties里面的属性就不会被加载进去了,若是你使用@Value,就会报Could not resolve placeholderthis
@Controller public class FirstController { @Value("${welcome.message}") private String test; @RequestMapping("/getw") public String welcome(Map<String, Object> model) { //model.put("message", this.message); System.out.println(test); return "my"; } }
这样使用就会报Could not resolve placeholderspa
解决:code
把db.properties的内容放到application.properties,而后这边引用:blog
<bean id="propertyPlaceholderConfigurer" class="...">
<property name="locations">
<list>
<value>classpath*:/application.properties</value>
<value>file:/etc/application.properties</value>
</list>
</property>
</bean>
或者两个文件都加载get
<bean id="propertyPlaceholderConfigurer" class="...">
<property name="locations">
<list>
<value>classpath*:/application.properties</value>
<value>classpath*:/db.properties</value>
<value>file:/etc/application.properties</value>
</list>
</property>
</bean>
缘由是spring的加载机制:Spring容器采用反射扫描的发现机制,在探测到Spring容器中有一个org.springframework.beans.factory.config.PropertyPlaceholderConfigurer的Bean就会中止对剩余PropertyPlaceholderConfigurer的扫描(Spring 3.1已经使用PropertySourcesPlaceholderConfigurer替代PropertyPlaceholderConfigurer了),因此根据加载的顺序,配置的第二个property-placeholder就被没有被spring加载,因此在使用@Value注入的时候占位符就解析不了io