javascript
Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer)
一、容器后處理器
Spring有如下幾個常用容器后處理器:
PropertyPlaceholderConfigurer:屬性點(diǎn)位符配置器
PropertyOverrideConfigurer:重寫占位符配置器
CustomAutowireConfigurer:自定義自動裝配的配置器
CustomScopeConfigurer:自定義作用域的配置器
容器后處理器用于負(fù)責(zé)處理容器本身,須實(shí)現(xiàn)BeanFactoryPostProcessor接口
并實(shí)現(xiàn)該接口的postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)方法。
如果采用ApplicationContext作為Spring容器,會自動注冊容器后處理器,如果采用BeanFactory作為Spring容器,則需要手動調(diào)用該容器來處理Spring容器。
代碼示例:
bean.xml配置文件代碼:
Java代碼:
//容器后處理器 public class MyClass implements BeanFactoryPostProcessor{ @Overridepublic void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory)throws BeansException {System.out.println("這是容器后處理器,名字是:"+beanFactory);}/*** 測試*/public static void main(String[] args) {//采用ApplicationContext作為Spring容器ApplicationContext app = new ClassPathXmlApplicationContext("bean.xml");User user = (User) app.getBean("user");//"user"參數(shù)是在bean.xml里注入U(xiǎn)ser類時的iduser.getMessage();}}如果用BeanFactory作為Spring容器,則需要在bean.xml里配置MyClass類,即手動注冊容器后處理器:
<bean id="myId" class="com.kting.agx.test.MyClass"/>二、屬性占位符配置器:PropertyPlaceholderConfigurer
該容器后處理器負(fù)責(zé)讀取Properties屬性文件里的屬性值,并將這些屬性值設(shè)置成Spring配置文年的元數(shù)據(jù),例如數(shù)據(jù)的URL、用戶名、密碼等等…..
base-conf-local.properties文件代碼:
com.kting.agx.base.dataSource0.jdbc.driverClassName=com.mysql.jdbc.Driver com.kting.agx.base.dataSource0.jdbc.url=jdbc:mysql://localhost:3306/agx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true com.kting.agx.base.dataSource0.jdbc.username=agx com.kting.agx.base.dataSource0.jdbc.password=agx987654com.kting.agx.base.dataSource1.jdbc.driverClassName=com.mysql.jdbc.Driver com.kting.agx.base.dataSource1.jdbc.url=jdbc:mysql://localhost/agx?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true com.kting.agx.base.dataSource1.jdbc.username=agx com.kting.agx.base.dataSource1.jdbc.password=agx987654bean.xml配置文件代碼:
<!-- 這樣配置后,可通過${}形式獲取前面properties文件里的值 --> <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"><property name="locations"><list><value>base-conf-local.properties</value><!-- 如果有多個配置文件,可依次列出來 : --><!-- <value>base-conf-online.properties</value> --></list></property> </bean> <bean id="write_dataSource" class="com.alibaba.druid.pool.DruidDataSource"parent="commonDataSourceProperties" init-method="init" destroy-method="close"><!-- 基本屬性 url、user、password --><property name="url" value="${com.kting.agx.base.dataSource0.jdbc.url}" /><property name="username" value="${com.kting.agx.base.dataSource0.jdbc.username}" /><property name="password" value="${com.kting.agx.base.dataSource0.jdbc.password}" /> </bean><bean id="read_dataSource" class="com.alibaba.druid.pool.DruidDataSource"parent="commonDataSourceProperties" init-method="init" destroy-method="close"><!-- 基本屬性 url、user、password --><property name="url" value="${com.kting.agx.base.dataSource1.jdbc.url}" /><property name="username" value="${com.kting.agx.base.dataSource1.jdbc.username}" /><property name="password" value="${com.kting.agx.base.dataSource1.jdbc.password}" /> </bean>如果在bean.xml文件的頭部分導(dǎo)入了context Schema:
xmlns:context="http://www.springframework.org/schema/context"則我們可通過如下方式配置該屬性占位符:
<context:property-placeholder location="classpath:base-conf-local.properties" />如果有多個properties文件時,可用通配符的形式
<context:property-placeholder location="classpath*:*-conf-local.properties" />三、重寫占位符配置器:PropertyOverrideConfigurer
總結(jié)
以上是生活随笔為你收集整理的Spring的两种容器后处理器(PropertyPlaceholderConfigurer和PropertyOverrideConfigurer)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring3.2新注解@Control
- 下一篇: Java设计模式详解