Life类
- package?com.open.bean; ??
- import?org.springframework.beans.BeansException; ??
- import?org.springframework.beans.factory.BeanFactory; ??
- import?org.springframework.beans.factory.BeanFactoryAware; ??
- import?org.springframework.beans.factory.BeanNameAware; ??
- import?org.springframework.beans.factory.DisposableBean; ??
- import?org.springframework.beans.factory.InitializingBean; ??
- public?class?Life?implements?BeanFactoryAware,?BeanNameAware, ??
- ????????InitializingBean,?DisposableBean?{ ??
- ????private?String?msg; ??
- ????public?Life()?{ ??
- ????????System.out.println("msg="+msg); ??
- ????????System.out.println("构造函数"); ??
- ????} ??
- ????public?void?setBeanFactory(BeanFactory?beanFactory)?throws?BeansException?{ ??
- ????????System.out.println("setBeanFactory"); ??
- ????} ??
- ????public?void?setBeanName(String?name)?{ ??
- ????????System.out.println("setBeanName"); ??
- ????} ??
- ????public?void?init()?{ ??
- ????????System.out.println("初始化"); ??
- ????} ??
- ????public?Object?postProcessBeforeInitialization(Object?bean,?String?beanName) ??
- ????????????throws?BeansException?{ ??
- ????????System.out.println("postProcessBeforeInitialization"); ??
- ????????return?null; ??
- ????} ??
- ????public?Object?postProcessAfterInitialization(Object?bean,?String?beanName) ??
- ????????????throws?BeansException?{ ??
- ????????System.out.println("postProcessAfterInitialization"); ??
- ????????return?null; ??
- ????} ??
- ????public?void?afterPropertiesSet()?throws?Exception?{ ??
- ????????System.out.println("afterPropertiesSet"); ??
- ????} ??
- ????public?void?destroy()?throws?Exception?{ ??
- ????????System.out.println("destroy"); ??
- ????} ??
- ????public?String?getMsg()?{ ??
- ????????return?msg; ??
- ????} ??
- ????public?void?setMsg(String?msg)?{ ??
- ????????this.msg?=?msg; ??
- ????} ??
- }??
package com.open.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanFactory; import org.springframework.beans.factory.BeanFactoryAware; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; public class Life implements BeanFactoryAware, BeanNameAware, InitializingBean, DisposableBean { private String msg; public Life() { System.out.println("msg="+msg); System.out.println("构造函数"); } public void setBeanFactory(BeanFactory beanFactory) throws BeansException { System.out.println("setBeanFactory"); } public void setBeanName(String name) { System.out.println("setBeanName"); } public void init() { System.out.println("初始化"); } public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization"); return null; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization"); return null; } public void afterPropertiesSet() throws Exception { System.out.println("afterPropertiesSet"); } public void destroy() throws Exception { System.out.println("destroy"); } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
?
?
BeanPostProcessorImp类
- package?com.open.bean; ??
- import?org.springframework.beans.BeansException; ??
- import?org.springframework.beans.factory.config.BeanPostProcessor; ??
- public?class?BeanPostProcessorImp?implements?BeanPostProcessor?{ ??
- ????public?Object?postProcessBeforeInitialization(Object?bean,?String?beanName) ??
- ????????????throws?BeansException?{ ??
- ????????System.out.println("postProcessBeforeInitialization"); ??
- ????????return?bean; ??
- ????} ??
- ????public?Object?postProcessAfterInitialization(Object?bean,?String?beanName) ??
- ????????????throws?BeansException?{ ??
- ????????System.out.println("postProcessAfterInitialization"); ??
- ????????return?bean; ??
- ????} ??
- }??
package com.open.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class BeanPostProcessorImp implements BeanPostProcessor { public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessBeforeInitialization"); return bean; } public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { System.out.println("postProcessAfterInitialization"); return bean; } }
?
?
BeanCounter类
- package?com.open.bean; ??
- import?org.springframework.beans.BeansException; ??
- import?org.springframework.beans.factory.config.BeanFactoryPostProcessor; ??
- import?org.springframework.beans.factory.config.ConfigurableListableBeanFactory; ??
- public?class?BeanCounter?implements?BeanFactoryPostProcessor?{ ??
- ????public?void?postProcessBeanFactory( ??
- ????????????ConfigurableListableBeanFactory?beanFactory)?throws?BeansException?{ ??
- ????????System.out.println("类的数量="+beanFactory.getBeanDefinitionCount()); ??
- ????} ??
- }??
package com.open.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; public class BeanCounter implements BeanFactoryPostProcessor { public void postProcessBeanFactory( ConfigurableListableBeanFactory beanFactory) throws BeansException { System.out.println("类的数量="+beanFactory.getBeanDefinitionCount()); } }
??
?
?
?
bean.xml
- <?xml?version="1.0"?encoding="UTF-8"?> ??
- <!DOCTYPE?beans?PUBLIC?"-//SPRING//DTD?BEAN//EN"??
- ????"http://www.springframework.org/dtd/spring-beans.dtd"> ??
- <beans> ??
- ????<bean?id="life"?name="life_name"?class="com.open.bean.Life"? ??
- ????????init-method="init"> ??
- ????????<property?name="msg"?value="lifexxxx"/> ??
- ????</bean>???? ??
- ????<bean?id="processor"?class="com.open.bean.BeanPostProcessorImp"/> ??
- ????<bean?id="counter"?class="com.open.bean.BeanCounter"/> ??
- </beans>??
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <bean id="life" name="life_name" class="com.open.bean.Life" init-method="init"> <property name="msg" value="lifexxxx"/> </bean> <bean id="processor" class="com.open.bean.BeanPostProcessorImp"/> <bean id="counter" class="com.open.bean.BeanCounter"/> </beans>
?
?
?
测试类
?
- package?com.open.bean; ??
- import?org.springframework.context.support.ClassPathXmlApplicationContext; ??
- public?class?Test?{ ??
- ????public?static?void?main(String[]?args)?{ ??
- ????????ClassPathXmlApplicationContext?cx= ??
- ????????????new?ClassPathXmlApplicationContext("bean.xml"); ??
- ?????????Life?life=(Life)cx.getBean("life");????? ??
- ????} ??
- }??????
package com.open.bean; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { ClassPathXmlApplicationContext cx= new ClassPathXmlApplicationContext("bean.xml"); Life life=(Life)cx.getBean("life"); } }
?
?
输出结果
?
类的数量=3
msg=null
构造函数
setBeanName
setBeanFactory
postProcessBeforeInitialization
afterPropertiesSet
初始化
postProcessAfterInitialization