Spring provides many PostProcessors for developers to expand, such as BeanPostProcessor, BeanFactoryPostProcessor, BeanValidationPostProcessor and other postprocessors. Most of their usage methods are similar. Understand one of them and master their usage methods, and the others can be understood by analogy.
Here we use BeanPostProcessor as an example to show how it is used.
The BeanPostProcessor interface provides two methods for developers to customize: postProcessBeforeInitialization and postProcessAfterInitialization.
postProcessBeforeInitialization: This method is mainly for spring to customize the process before calling the initialization method when bean is initialized.
postProcessAfterInitialization: This method is mainly for spring to customize the process after calling the initialization method when bean initialization.
Sample code:
/** * Test bean */public class Cat { private String name; private int age; public void say() { System.out.println("name:" + name); System.out.println("age:" + age); } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }} /** * Custom postprocessor*/public class CatBeanPostProcessor implements BeanPostProcessor { @Nullable @Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof Cat) { //Output original attribute Cat cat = (Cat) bean; cat.say(); return bean; } return bean; } @Nullable @Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { if (bean instanceof Cat) { //Modify the attribute value and return Cat cat = (Cat) bean; cat.setName("hello maomi"); cat.setAge(3); return cat; } return bean; }} /** * Run */public class Run { public static void main(String[] args) { ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-bean.xml"); Cat cat = (Cat) applicationContext.getBean("cat"); cat.say(); }}xml configuration information
<!--Configure bean and initialize--> <bean id="cat" > <property name="name" value="HelloKitty" /> <property name="age" value="1" /> </bean> <bean id="catBeanPostProcessor" />
Output result:
name:HelloKitty
age:1
name:hello maomi
age:3
You can see that the bean information processed through the postprocessor has changed. Finally, let's take a look at how to call custom implementations in the source code.
In the initialization bean method: AbstractAutowireCapableBeanFactory.java
/** * Initialization bean */ protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) { //Omit part of the irrelevant code Object wrappedBean = bean; //Before initialization if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { //Calling the initialization method to initialize bean invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } //If (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; } //The postProcessBeforeInitialization method calls @Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { //Calling the custom postProcessBeforeInitialization method Object current = beanProcessor.postProcessBeforeInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; } //postProcessAfterInitialization method call @Override public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { //Custom postProcessAfterInitialization method calls Object current = beanProcessor.postProcessAfterInitialization(result, beanName); if (current == null) { return result; } result = current; } return result; }The above is the calling process of spring to implement custom methods. I hope it will be helpful to everyone's learning, and I hope everyone will support Wulin.com more.