`
无量
  • 浏览: 1133428 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

Spring中@Value注解,需要注意的地方

阅读更多
Spring 3以后,支持@Value注解的方式获取properties文件中的配置值,简化了读取配置文件的复杂操作

1、在applicationContext.xml文件(或引用文件中)中配置properties文件
<bean id="appProperty"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations">
        <array>
            <value>classpath:config.properties</value>
        </array>
    </property>
</bean>


2、在bean中使用@value注解获取配置文件的值
@Value("${ftp.name}")
private String ftpName;


注意:
1.即使给变量赋了初值也会以配置文件的值为准

2.用@Value注解只能,在spring管理的bean中使用,在非spring管理的工具类中,只能通过静态方法读取配置文件
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics