码迷,mamicode.com
首页 > 编程语言 > 详细

spring 3 的 @value的使用

时间:2016-02-03 11:45:16      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

Spring 3支持@value注解的方式获取properties文件中的配置值,大大简化了我们读取配置文件的代码。使用方式如下:

1.首先必须要配置properties文件的加载bean:在spring的配置文件中加入:让spring将properties的文件中的内容加载进spring容器中.

<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("${name}")
private Boolean timerEnabled;

3.一般把@value的值,取自于propertise文件,而propertise文件的变量值来源于maven的变量替换,以便于达到不同环境(sit\pre\prd)的不同配置的目的.

常常运用于接口的发送配置变量.http发送的接收地址(url)等等.

 

spring 3 的 @value的使用

标签:

原文地址:http://www.cnblogs.com/lilin0719/p/5179177.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!