JAVA spring 把全局变量写到配置文件中

把一些全局的参数配置到配置文件里面,把全局属性注入到类里面,由程序代码直接引用.

普通引入properties方法(只介绍)

在spring的配置文件applicationContext.xml配置

 
 
<bean id= "propertyConfigurer" class= "org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:config.properties</value> </list> </property> < /bean>

改进后的properties引入方法

在spring的配置文件applicationContext.xml配置

 
 
<bean id= "configProperties" class= "org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="locations"> <list> <value>classpath*:config.properties</value> </list> </property> < /bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PreferencesPlaceholderConfigurer"> <property name="properties" ref="configProperties"></property> </bean>

config.properties文件配置内容

扫描二维码关注公众号,回复: 3593712 查看本文章

# 变量
maxid=12342

JAVA类的使用示例

 
 
@Value( "#{configProperties['maxid']}") private int maxid;

猜你喜欢

转载自blog.csdn.net/Pagegle/article/details/79139220