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管理的工具类中,只能通过静态方法读取配置文件

猜你喜欢

转载自825635381.iteye.com/blog/2196906