Spring中后台读取配置文件信息
有些配置信息,不同的环境,配置信息不同,需要在后端使用这些信息,使用Spring3支持的@value注解的方式获取properties文件中的配置值,大大简化了读取配置文件的代码。
步骤:
- 在applicationContext.xml(spring的配置文件,不要加入到springmvc的配置文件中)文件中配置properties文件
<bean id="configProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:conf/main-setting-web.properties</value>
</list>
</property>
</bean>
2.main-setting-web.properties文件中增加参数
auth.server.url=$[authServerUrl]
server.name=$[serverName]
authServerUrl,serverName是在maven中配置的
3.controller中使用
@Value("#{configProperties['auth.server.url']}")
private String authServerUrl;
@Value("#{configProperties['server.name']}")
private String serverName;
configProperties为applicationContext.xml中配置的bean id