使用注解注入properties中的值的简单示例 spring使用注解注入properties中的值的简单示例

 

1.在web项目的src目录下新建setting.properties的文件,内容如下:

version=1

2.在spring的xml配置文件中加入以下配置:

<!-- 使用注解注入properties中的值 -->

   <bean >

       <property name="locations">

           <list>

               <value>classpath:setting.properties</value>

           </list>

       </property>

       <!-- 设置编码格式 -->

       <property name="fileEncoding" value="UTF-8"></property>

   </bean>

3.在controller类中加入以下注解即可

  @Value("#{setting[version]}")

   private String version;