"Spring-source analysis" notes
BeanPostProcessor principle of learning
After learning the principles of BeanPostProcessor finished learning how to use Spring curious, try to understand using examples, the following recording:
1, using the ApplicationContextAware, can be specified, the container obtained in the current function context, the specific use for example as follows:
@Component public class Dog implements ApplicationContextAware { //@Autowired private ApplicationContext context; public Dog(){ System.out.println("dog constructor..."); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { // TODO Auto-generated method stub this.context = applicationContext; } }
2, you can get by using a Spring container context variable context, but how to achieve the Dog class?
ApplicationContextAware itself does not inherit BeanPostProcess, only inherited Aware Interface
public interface ApplicationContextAware extends Aware { /** * Set the ApplicationContext that this object runs in. * Normally this call will be used to initialize the object. * <p>Invoked after population of normal bean properties but before an init callback such * as {@link org.springframework.beans.factory.InitializingBean#afterPropertiesSet()} * or a custom init-method. Invoked after {@link ResourceLoaderAware#setResourceLoader}, * {@link ApplicationEventPublisherAware#setApplicationEventPublisher} and * {@link MessageSourceAware}, if applicable. * @param applicationContext the ApplicationContext object to be used by this object * @throws ApplicationContextException in case of context initialization errors * @throws BeansException if thrown by application context methods * @see org.springframework.beans.factory.BeanInitializationException */ void setApplicationContext(ApplicationContext applicationContext) throws BeansException; }
By running the code, you can see a call, the setApplicationContext is achieved through the class ApplicationContextAwareProcessor,
View ApplicationContextAwareProcessor:
class ApplicationContextAwareProcessor implements BeanPostProcessor {}
When the container assignment Bean, will traverse the BeanPostProcessor container, one by one executed, execution will naturally postProcessBeforeInitialization () method to the ApplicationContextAwareProcessor.
Where execution is this.invokeAwareInterfaces (bean) function, see invokeAwareInterfaces () Function Code:
private void invokeAwareInterfaces(Object bean) { if (bean instanceof Aware) { if (bean instanceof EnvironmentAware) { ((EnvironmentAware)bean).setEnvironment(this.applicationContext.getEnvironment()); } if (bean instanceof EmbeddedValueResolverAware) { ((EmbeddedValueResolverAware)bean).setEmbeddedValueResolver(this.embeddedValueResolver); } if (bean instanceof ResourceLoaderAware) { ((ResourceLoaderAware)bean).setResourceLoader(this.applicationContext); } if (bean instanceof ApplicationEventPublisherAware) { ((ApplicationEventPublisherAware)bean).setApplicationEventPublisher(this.applicationContext); } if (bean instanceof MessageSourceAware) { ((MessageSourceAware)bean).setMessageSource(this.applicationContext); } if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware)bean).setApplicationContext(this.applicationContext); } } }
3, it can be seen from the code
This function is performed only achieve Aware Interface Bean, our implementation class Dog realize that ApplicationContextAware
That will execute its associated code automatically assigned Dog variables.