【SpringMVC】SpringMVC获取配置文件信息

版权声明:【本文为博主原创文章或收集整理,未经博主允许不得转载】 https://blog.csdn.net/zsq520520/article/details/72676106

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,完成。

猜你喜欢

转载自blog.csdn.net/zsq520520/article/details/72676106