Spring MVC 通过@Value注解读取.properties配置内容

王朝学院·作者佚名  2016-05-29
窄屏简体版  字體: 小  |  中  |  大  |  超大  

SPRing MVC 通过@Value注解读取.properties配置内容第一步:

在applicationContext.xml配置:

1 <bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> 2 <property name="locations"> 3 <list> 4 <value>classpath:/config/*.properties</value> 5 </list> 6 </property> 7 </bean> 8 <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> 9 <property name="properties" ref="configProperties" />10 </bean>

第二步:

建立配置文件内容:

例如:userPageSize=5

第三步:

在Controller中使用注解获得配置项内容:

1 @Value("#{configProperties['userPageSize']}")2 private String userPageSize;

第四步:

后面的代码就可以使用userPageSize这个私有变量了,这个字符串的值就是我们配置文件中配置的5.

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航