1, after extended use IoC container extension processor
bean post-processor: After the bean container processing, which is the additional reinforcement.
After the container treatment: After IoC container processing, enhanced vessel function.
2, bean postprocessor
After BeanPostProcessor bean processor must implement the interface, there is provided a method of non-:
Object postProcessBeforeInitialization (Object bean, String name) throws BenasException: The first parameter is the method bean instance the system is about to be processed, the second parameter is the bean configuration id. Be called back before the target bean initialization.
Object postProcessAfterInitialization (Object bean, String name) throws BenasException: The first parameter is the method bean instance the system is about to be processed, the second parameter is the bean configuration id. It is called back after the target bean initialization.
for example:
MyBeanPostProcessor.java
package com.lfy.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanPostProcessor; public class MyBeanPostProcessor implements BeanPostProcessor { @Override public Object postProcessAfterInitialization(Object bean, String name) throws BeansException { System.out.println("bean后处理器在初始化之后对"+name+"进行增强处理..."); return bean; } @Override public Object postProcessBeforeInitialization(Object bean, String name) throws BeansException { System.out.println ( "bean post processor before initiating the" + name + "enhancement processing ..." ); return bean; } }
Chinese.java
package com.lfy.bean; import org.springframework.beans.BeansException; import org.springframework.beans.factory.BeanNameAware; import org.springframework.beans.factory.DisposableBean; import org.springframework.beans.factory.InitializingBean; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; public class Chinese implements InitializingBean,DisposableBean,BeanNameAware,ApplicationContextAware { private ApplicationContext ctx; private String beanID; private String someBodyName; public Chinese() { System.out.println("-----无参构造器-----"); } @Override public void setApplicationContext(ApplicationContext ctx) throws BeansException { this.ctx=ctx; System.out.println("-----获取ApplicationContext容器ctx-----"); } @Override public void setBeanName(String name) { this.beanID=name; System.out.println("-----获取bean id-----"); } /** * Lifecycle methods afterPropertiesSet * / @Override public void afterPropertiesSet () throws Exception { System.out.println ( "----- after dependency injection, lifecycle methods ----- afterPropertiesSet" ); } / ** * lifecycle methods the init * / public void the init () { System.out.println ( "----- dependency after injection, the lifecycle methods ----- the init" ); } / ** * method lifecycle Destory * / @Override public void the destroy () throws Exception { System.out.println ( "bean destroyed before the destroy ----- -----"); } / ** * lifecycle methods Close * / public void Close () { System.out.println ( "Close ----- ----- destroyed before bean" ); } / ** * the setter method * @param name * / public void setSomeBodyName (String name) { the this .someBodyName = name; System.out.println ( "----- attribute property setter methods injection -----" + someBodyName); } }
beans.xml
<?xml version="1.0" encoding="UTF-8"?> <!-- spring配置文件的根元素,使用spring-beans-4.0.xsd语义约束 --> <beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd"> <bean id="chinese" class="com.lfy.bean.Chinese" init-method="init" destroy-method="close"> <property name="someBodyName" value="至尊宝"/> </bean> <bean class="com.lfy.bean.MyBeanPostProcessor"/> </beans>
SpringTest.java
package com.lfy.main; import org.springframework.context.support.AbstractApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.lfy.bean.Chinese; /** * * @author lfy * */ public class SpringTest { public static void main(String[] args) { AbstractApplicationContext ctx=new ClassPathXmlApplicationContext("beans.xml"); Chinese chin=ctx.getBean("chinese", Chinese.class); ctx.registerShutdownHook(); System.exit(0); } }
operation result:
Summary: container automatically implements the interface bean BeanPostProcessor registered as a post-processor. Performing the above sequence between them. After processing is executed automatically at each bean is created.
BeanFactory spring used as a container, must manually register the processor.
3, the container postprocessor