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

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

 

 

 

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

version=1

 

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

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

   <bean id="setting" class="org.springframework.beans.factory.config.PropertiesFactoryBean">

       <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;

posted on 2018-12-28 13:39  我是司  阅读(1365)  评论(0编辑  收藏  举报

导航