【Spring】学习@Value注解

在spring 3.0中,可以通过使用@value,对一些如xxx.properties文件中的文件,进行键值对的注入,例子如下:

目录
目录

  1. 首先在applicationContext.xml中加入:

    <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"><property name="locations"><list><value>classpath:valueTest.properties</value></list></property>
    </bean>
  2. 新建valueTest.properties文件

    name=redgo
    age=21
  3. 在Controller , Service中使用

    @Value("#{configProperties['name']}")
    private String name;
    @Value("#{configProperties.age}")
    private String age;
  4. 运行测试