Spring 属性文件的注入管理

           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}")

我们在实际使用中一般都是使用第二种方式,特别是配置数据库的连接参数上,使用的比较多,今天就总结这么多了,在使用方式上,其实这两个类可以组合使用,具体怎么用需要根据自己的使用情况了。







  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值