Spring 引入属性文件

来源:互联网 发布:淘宝客店铺商品采集 编辑:程序博客网 时间:2024/06/05 19:39

示例代码:
在配置类上写上 @PropertySource

@PropertySource("classpath:application.properties")

注入成员变量的时候使用 @Value("${jdbc.jdbcUrl}")

另外,一定要注册一个 PropertySourcesPlaceholderConfigurer

@Beanpublic 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

0 0
原创粉丝点击