使用Spring 3的@value简化配置文件的读取

来源:互联网 发布:社交网络强弱关系 编辑:程序博客网 时间:2024/06/05 11:32
Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码。

1、在applicationContext.xml文件中配置properties文件

<bean id="appProperty"    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">    <property name="locations">        <array>            <value>classpath:app.properties</value>        </array>    </property></bean>

2、在bean中使用@value注解获取配置文件的值

@Value("${chengmi_crawl_timer_enable}")private Boolean timerEnabled;

即使给变量赋了初值也会以配置文件的值为准。

阅读全文
0 0
原创粉丝点击