【SpringMVC】SpringMVC获取配置文件信息
1、首先新建一个.properties的配置文件。如:config.properties
#file.acpath.server=c:/files
#file.acpath.views.server=c:/files/%s/%s?t=%s
server.file.acpath=
file.acpath.server=
file.acpath.views.server=
2、在applicationContext.xml中配置。如下:
<!-- 加载配置文件 -->
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="order" value="1"></property>
<property name="ignoreUnresolvablePlaceholders" value="true"></property>
<property name="locations">
<list>
<value>classpath:config.properties</value>
</list>
</property>
</bean>
<!--配置全局变量-->
<bean id="properties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="singleton" value="true"/>
<property name="properties">
<props>
<prop key="file.acpath.server">${file.acpath.server}</prop>
<prop key="file.acpath.views.server">${file.acpath.views.server}</prop>
<prop key="server.file.acpath">${server.file.acpath}</prop>
</props>
</property>
</bean>
3、在controller层引入即可获取到。
@Autowired
protected Properties properties;
获取值:
String fileACPath = properties.getProperty("file.acpath.views.server");
ok,完成。
还没有评论,来说两句吧...