BeanPostProcessor
/** * The top-level post-handler, you can see that the argument is a bean object, representing the processing of a bean object. As you can see from the name of the method *, this post-handler will be called after the bean object is created, because the two methods have a bean object */ as an input parameter
public interface BeanPostProcessor {
default Object postProcessBeforeInitialization(Object bean, String beanName) {
return bean;
}
default Object postProcessAfterInitialization(Object bean, String beanName) {
returnbean; }}Copy the code
InstantiationAwareBeanPostProcessor
/** * This post-handler inherits the top-level post-handler and adds several methods. The first two methods are called before and after the bean is instantiated, respectively, and the last method is used to process attributes, which can be interpreted as a pair@AutowiredAnnotation handling */
public interface InstantiationAwareBeanPostProcessor extends BeanPostProcessor {
default Object postProcessBeforeInstantiation(Class
beanClass, String beanName) {
return null;
}
default boolean postProcessAfterInstantiation(Object bean, String beanName) {
return true;
}
default PropertyValues postProcessProperties(PropertyValues pvs, Object bean, String beanName) {
return null; }}Copy the code
Simple summary
The before method of BeanPostProcessor is typically used to perform initialization actions after the bean object has been created, such as @PostContructor annotations and method calls to the InitializeBean interface. So long as bean object is created, but after the realization of the method is the most typical applications of AOP, Spring is in the method after completed AOP implementation InstantiationAwareBeanPostProcessor former two interfaces, As you can see, the before method takes a beanClass as an argument, so you can assume that the bean object has not yet been created. This method is provided because Spring provides a means to call the before method before actually creating the bean. Allows programmers to write proxy logic on a beanClass object. In other words, if this method returns an object, the logic is written by the programmer, and Spring will no longer use its own bean creation logic. The after method returns a Boolean indicating whether the bean object has been instantiated. The postProcessProperties method handles property injection, such as the @AutoWired annotationCopy the code
SmartInstantiationAwareBeanPostProcessor
public interface SmartInstantiationAwareBeanPostProcessor
extends InstantiationAwareBeanPostProcessor {
defaultClass<? > predictBeanType(Class<? > beanClass, String beanName) {return null;
}
defaultConstructor<? >[] determineCandidateConstructors(Class<? > beanClass, String beanName) {return null;
}
default Object getEarlyBeanReference(Object bean, String beanName) {
returnbean; }} analysis: Rear SmartInstantiationAwareBeanPostProcessor processor inherited InstantiationAwareBeanPostProcessor and provides three methods, The predictBeanType method is used to predict the type of bean, because the bean created in a beanDefinition is not necessarily the beanClass object in that definition, as in the case of FactoryBean. DetermineCandidateConstructors method is used to create bean object inference method to construct, when can easily think of is, in fact, when we create bean object but is the way that USES reflection to create, so an object may have many a constructor, When creating bean exactly which construct method, is by the rear determineCandidateConstructors method to determine the processor, getEarlyBeanReference method is used to solve the circular dependencies, This step can be thought of as a level 2 cache in circular dependencies, and we'll talk more about this implementation when we talk about circular dependenciesCopy the code
Conclusion: There are other post-processor interfaces, but those are not as important. Spring instantiates beans mainly through the implementation classes of these interfaces. Understanding their relationship ahead of time will help with bean instantiation and AOP later on