spring核心:bean工厂的装配 6
本文中主要包含:
23.使用后臺處理器post-processor
1.使用BeanPostProcessor
如果向一個bean factory注冊post-processor的話,那么對于這個工廠產(chǎn)生的每個bean的實例的話,都會調(diào)用Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException和Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException方法。這里需要注意的是ApplicationContext會自動檢測并注冊post-processor,但是BeanFactory需要手動注冊。使用方法如下:
MyBeanPostProcessor.java:
/** * */ package beanfactory.postprocessor; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; import beanfactory.SimpleBean; /** * @author jefferyxu * */ public class MyBeanPostProcessor implements BeanPostProcessor { /* (non-Javadoc) * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessAfterInitialization(java.lang.Object, java.lang.String) */ @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { ((SimpleBean)bean).setMsg("i am called by the postProcessAfterInitialization."); return bean; } /* (non-Javadoc) * @see org.springframework.beans.factory.config.BeanPostProcessor#postProcessBeforeInitialization(java.lang.Object, java.lang.String) */ @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { ((SimpleBean)bean).setMsg("i am called by the postProcessBeforeInitialization"); return bean; } }
客戶端程序:
Resource resource = new ClassPathResource("applicationContext.xml"); ConfigurableBeanFactory context = new XmlBeanFactory(resource); BeanPostProcessor postProcessor = new MyBeanPostProcessor(); context.addBeanPostProcessor(postProcessor); SimpleBean simpleBean = (SimpleBean)context.getBean("simpleBean"); System.out.println(simpleBean.getMsg());
2.使用BeanFactoryPostProcessor定制bean工廠
如果一個類實現(xiàn)上面的接口,那么這個類就能在bean factory創(chuàng)建之后,完成對于這個bean factory的修改。spring中包含很多現(xiàn)成的實現(xiàn)上面接口的實現(xiàn)類:PropertyPlaceholderConfigurer,PropertyOverrideConfigurer。下面是這兩個的實例。
1.PropertyPlaceholderConfigurer
如果在applicationContext.xml文件中定義:
<bean id="myDataSource" class="beanfactory.postprocessor.MyDataSource" abstract="false" lazy-init="default" autowire="default" p:username="${jdbc.username}" p:dirverClassName="${jdbc.driverClassName}" p:pasword="${jdbc.password}" p:url="${jdbc.url}"> </bean>
同時存在下面的文件jdbc.properties:
jdbc.driverClassName=myDriverName jdbc.username=jefferyxu jdbc.password=password jdbc.url=url
客戶端:
package beanfactory.postprocessor; import org.springframework.beans.factory.config.ConfigurableBeanFactory; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; import org.springframework.beans.factory.config.PropertyResourceConfigurer; import org.springframework.beans.factory.xml.XmlBeanFactory; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; public class BeanFactoryPropPlaceHolderUsage { public static void main(String[] args) { Resource resource = new ClassPathResource("applicationContext.xml"); ConfigurableBeanFactory context = new XmlBeanFactory(resource); // 根據(jù)配置文件生成PropertyResourceConfigurer PropertyResourceConfigurer propertyResourceConfigurer = new PropertyPlaceholderConfigurer() ; resource = new ClassPathResource("jdbc.properties"); propertyResourceConfigurer.setLocation(resource); // 執(zhí)行真正的替換 propertyResourceConfigurer.postProcessBeanFactory((ConfigurableListableBeanFactory) context); // 去得配置之后的datasource MyDataSource dataSource = (MyDataSource)context.getBean("myDataSource"); System.out.println(dataSource.getDirverClassName()); } }
轉(zhuǎn)載于:https://www.cnblogs.com/xuqiang/archive/2010/10/21/1953444.html
總結(jié)
以上是生活随笔為你收集整理的spring核心:bean工厂的装配 6的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: SQL Server 2008将数据导出
- 下一篇: javascript中的继承方式