spring管理属性配置文件properties,为我们提供了两个类,PropertiesFactoryBean和PropertyPlaceholderConfigurer,这两个类都可以实现属性文件的管理,首先我们来看一下两个类的配置。
-----------------------------------------------------------------------------------------------------------------
<!-- PropertiesFactoryBean -->
<bean id="setting"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>file:config/*.conf</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
-----------------------------------------------------------------------------------------------------------------
<!-- PropertyPlaceholderConfigurer-->
<bean id="setting"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>file:config/*.conf</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
-------------------------------------------------------------------------------------------------------------------------
通过配置其实我们能看出来,这两个类的使用方式应该差不多,实际上却是是这样的。我们在使用的时候,Spring为我们提供了一个注解@Value这个注解可以为我们将属性注入,这样我们就可以很便捷的读取配置文件的属性了。
说完配置了,我们在来说说如何使用吧,使用过程中我们一般就是通过@Value注解了,但是实际的使用方式上,取值的方式方法上还是有很多不同的,主要的区别是1.PropertiesFactoryBean
@Value("#{beanid[proper_name]}")
2.PropertyPlaceholderConfigurer
@Value("${proper_name}")
我们在实际使用中一般都是使用第二种方式,特别是配置数据库的连接参数上,使用的比较多,今天就总结这么多了,在使用方式上,其实这两个类可以组合使用,具体怎么用需要根据自己的使用情况了。
-----------------------------------------------------------------------------------------------------------------
<!-- PropertiesFactoryBean -->
<bean id="setting"
class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>file:config/*.conf</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
-----------------------------------------------------------------------------------------------------------------
<!-- PropertyPlaceholderConfigurer-->
<bean id="setting"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="ignoreUnresolvablePlaceholders" value="true" />
<property name="locations">
<list>
<value>classpath:*.properties</value>
<value>file:config/*.conf</value>
</list>
</property>
<property name="fileEncoding" value="UTF-8"></property>
</bean>
-------------------------------------------------------------------------------------------------------------------------
通过配置其实我们能看出来,这两个类的使用方式应该差不多,实际上却是是这样的。我们在使用的时候,Spring为我们提供了一个注解@Value这个注解可以为我们将属性注入,这样我们就可以很便捷的读取配置文件的属性了。
说完配置了,我们在来说说如何使用吧,使用过程中我们一般就是通过@Value注解了,但是实际的使用方式上,取值的方式方法上还是有很多不同的,主要的区别是1.PropertiesFactoryBean
@Value("#{beanid[proper_name]}")
2.PropertyPlaceholderConfigurer
@Value("${proper_name}")
我们在实际使用中一般都是使用第二种方式,特别是配置数据库的连接参数上,使用的比较多,今天就总结这么多了,在使用方式上,其实这两个类可以组合使用,具体怎么用需要根据自己的使用情况了。