spring callback

 

     Spring defines three interfaces that can be used to process Spring beans or BeanFactory that generates beans , InitializingBean , BeanPostProcessor and BeanFactoryPostProcessor .

 

An afterPropertiesSet() method is defined in      InitializingBean . When the BeanFactory instantiates our bean and sets the corresponding properties, if our bean implements the InitializingBean interface, the corresponding afterPropertiesSet() method will be called. Then we can change the properties of the current bean and other operations in this method body.

@Component("beanA")
public class BeanA implements InitializingBean {
/**
 * Callback function, which will be called after the bean property is set
 */
	public void afterPropertiesSet() throws Exception {
		System.out.println("Callback function, will be called after the bean properties are set");
	}
}

 

     The BeanPostProcessor interface implementation class can do some processing on the bean before and after the bean is initialized . ApplicationContext can automatically detect whether the bean in it has implemented the BeanPostProcessor interface. If the interface has been implemented, it will automatically treat it as a BeanPostProcessor . Then call it when you need to call the BeanPostProcessor . Two methods are defined in BeanPostPorcessor , postProcessBeforeInitialization() and postProcessAfterInitialization() .

l   postProcessBeforeInitialization(Object bean, String beanName) method will be called before calling the bean 's initialization method. The method parameters represent the current bean object and the corresponding bean name respectively.

l   postProcessAfterInitialization(Object bean, String beanName) method will be called after the initialization method of the bean is called.

     BeanPostProcessor is for all beans in the container. Once a BeanPostProcessor is defined in the container, each bean in the container will call the corresponding method of the BeanPostProcessor before and after initialization .

public class MyBeanPostProcessor implements BeanPostProcessor {
/**
 * Any bean object will call the BeanPostProcessor after the initialization method callback
 * postProcessAfterInitialization method. We can encapsulate the returned bean in the body of the method.
 * Before calling this method, the bean object we pass in is already filled with property values. When we put the BeanPostProcessor as
 * When a bean is defined in ApplicationContext, ApplicationContext will automatically detect it and treat it as
 * A BeanPostProcessor to call.
 */
	public Object postProcessAfterInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println(bean + "after initialization, beanName is " + beanName);
		return bean;
	}

/**
 * Any bean object will call the BeanPostProcessor before the initialization method callback
 * postProcessBeforeInitialization method. Before calling this method, we pass in the
 * The bean object is already populated with property values.
 */
	public Object postProcessBeforeInitialization(Object bean, String beanName)
			throws BeansException {
		System.out.println(bean + "beforeInitialization, beanName is " + beanName);
		return bean;
	}
}

  

 

    The BeanFactoryPostProcessor interface implementation class can do some processing on the BeanFactory after the current BeanFactory is initialized and before the bean is instantiated. BeanFactoryPostProcessor is aimed at the bean container. When calling it, the BeanFactory only loads the definition of the bean, and has not instantiated them, so we can achieve the effect of instantiating the bean by processing the BeanFactory. Like BeanPostProcessor, ApplicationContext can also automatically detect and call BeanFactoryPostProcessor in the container.  

@Component
public class MyBeanFactoryPostProcessor implements BeanFactoryPostProcessor {
/**
* The postProcessBeanFactory() method of BeanFactoryPostProcessor will be initialized in the current BeanFactory
* later, and all bean definitions have been loaded, but there is no corresponding instance called.
* So we can do some operations through BeanFactory in the method body. When we put BeanFactoryPostProcessor
* When defined in ApplicationContext as a bean, ApplicationContext will automatically detect it and treat it as
* A BeanFactoryPostProcessor to call.
*/
	public void postProcessBeanFactory(
		ConfigurableListableBeanFactory beanFactory) throws BeansException {
		System.out.println("postProcessBeanFactory......");
	}
}

 

 

 

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326960446&siteId=291194637