使用Spring 3的@value简化配置文件的读取
Spring 3支持@value注解的方式获取properties文件中的配置值,大简化了读取配置文件的代码。
1、在applicationContext.xml文件中配置properties文件
<bean id="configPropertys" class="org.springframework.beans.factory.config.PropertysFactionBean"> <property name="locations"> <list> <value>classpath*:system.properties</value>
<value>classpath*:db.properties</value>
</list> </property> </bean>
2、在controller中使用@value注解获取配置文件的值
@Value("#{configPropertys['server-oa.web.uploadPath']}")
private String uploadPath;
即使给变量赋了初值也会以配置文件的值为准。