示例代码:
在配置类上写上 @PropertySource
@PropertySource("classpath:application.properties")
注入成员变量的时候使用 @Value("${jdbc.jdbcUrl}")
另外,一定要注册一个 PropertySourcesPlaceholderConfigurer
@Bean
public static PropertySourcesPlaceholderConfigurer propertyConfigure() {
return new PropertySourcesPlaceholderConfigurer();
}
如果引入的是 Spring 的核心配置文件,在类上写上注解:
@ImportResource(value="classpath:applicationContext.xml")
在核心配置文件里面引入属性文件的两种方法:
第 1 种:
<context:property-placeholder location="classpath:application.properties" />
第 2 种:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<value>classpath:jdbc.properties</value>
</property>
</bean>
参考资料:
http://blog.chinaunix.net/uid-26209648-id-2435959.html