属性处理器Spring攻略学习笔记(2.12)外部化Bean配置

PS:今天上午,非常郁闷,有很多单简基础的问题搞得我有些迷茫,哎,码代几天不写就忘。目前又不当COO,还是得用心记码代哦!

    一、知识点  

            Spring中PropertyPlaceholderConfigurer这个Bean厂工后处理器,用来将分部Bean配置独自放到一个属性文件中。可以在Bean配置文件中应用${var}式形的量变,PropertyPlaceholderConfigurer将从属性文件中载加属性并且用它们替换量变。

            Bean厂工后处理器与Bean后处理器之间的不同是它的目标是IoC容器---------Bean厂工或者应用程序上下文,而不是Bean实例。Bean厂工后处理器将在IoC容器载加Bean配置以后、Bean实例创立之前失效,它的型典途用是在Bean实例化之前改修Bean配置。Spring有多个Bean厂工后处理器。

    二、码代示例

          属性文件config.properties,放置路径

cashier.path=c:/cashier

        

    Bean配置

<!-- 注册Bean厂工后处理器PropertyPlaceholderConfigurer,在location属性配置一个属性文件,还可以给locations属性配置多个属性文件  -->
    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    	<property name="location">
    	    <value>config.properties</value>	
    	</property>
    </bean>
    
    <bean id="cashier1" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Cashier" init-method="openFile" destroy-method="closeFile" >
    	<property name="path" value="${cashier.path}" />
    </bean>
    每日一道理
生命,是一场漫长的棋局。这盘棋没有猎猎西风,没有四起狼烟,只有在取舍和进退中抉择。只有像棋中的小卒那样,勇往直前,毫不退缩沿着沟沟坎坎的人生之路,艰难而执着的求索,前进,才会谱写人生最壮丽的强者之歌。

      在Spring2.5及更高版本中可以通过<context:property-placeholder>元素单简地注册PropertyPlaceholderConfigurer。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/beans/spring-context-3.2.xsd">   
    <!-- 注册Bean后处理器 -->
    <bean class="com.codeproject.jackie.springrecipesnote.springadvancedioc.PathCheckingBeanPostProcessor" /> 
    。。。。。。。。。。。
    
    <context:property-placeholder location="config.properties" />
    
    <bean id="cashier1" class="com.codeproject.jackie.springrecipesnote.springadvancedioc.Cashier" init-method="openFile" destroy-method="closeFile" >
    	<property name="path" value="${cashier.path}" />
    </bean> 
</beans>

文章结束给大家分享下程序员的一些笑话语录: 警告
有一个小伙子在一个办公大楼的门口抽着烟,一个妇女路过他身边,并对他 说, “你知道不知道这个东西会危害你的健康?我是说, 你有没有注意到香烟 盒上的那个警告(Warning)?”
小伙子说,“没事儿,我是一个程序员”。
那妇女说,“这又怎样?”
程序员说,“我们从来不关心 Warning,只关心 Error”

原文地址:https://www.cnblogs.com/xinyuyuanm/p/3045561.html