How does ApplicationContextAware work in Spring?

In spring, if the bean implements ApplicationContextAware , then it can access the applicationContext . Therefore, it can get other beans. eg

 public class SpringContextUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext context) throws BeansException { applicationContext = context; } public static ApplicationContext getApplicationContext() { return applicationContext; } } 

Then SpringContextUtil.getApplicationContext.getBean("name") can get the name bean ".

To do this, we must put this SpringContextUtil inside applications.xml , for example.

 <bean class="com.util.SpringContextUtil" /> 

Here, the SpringContextUtil bean SpringContextUtil not include the applicationContext property. I think when spring bean initialize, this property is set. But how is this done? How is the setApplicationContext method setApplicationContext ?

+56
java spring
Feb 04 '14 at 12:52
source share
3 answers

When spring instantiates the beans, it looks for a couple of interfaces, such as ApplicationContextAware and InitializingBean . If found, methods are called. For example. (very simplified)

 Class<?> beanClass = beanDefinition.getClass(); Object bean = beanClass.newInstance(); if (bean instanceof ApplicationContextAware) { ((ApplicationContextAware) bean).setApplicationContext(ctx); } 

Note that in the newer version it is better to use annotations rather than using spring-specific interfaces. Now you can simply use:

 @Inject // or @Autowired private ApplicationContext ctx; 
+62
Feb 04 '14 at 13:02
source share

Spring source code to explain how ApplicationContextAware works
when you use ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
In the AbstractApplicationContext class, the refresh() method has the following code:

 // Prepare the bean factory for use in this context. prepareBeanFactory(beanFactory); 

enter this method, beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); Add ApplicationContextAwareProcessor to AbstractrBeanFactory.

 protected void prepareBeanFactory(ConfigurableListableBeanFactory beanFactory) { // Tell the internal bean factory to use the context class loader etc. beanFactory.setBeanClassLoader(getClassLoader()); beanFactory.setBeanExpressionResolver(new StandardBeanExpressionResolver(beanFactory.getBeanClassLoader())); beanFactory.addPropertyEditorRegistrar(new ResourceEditorRegistrar(this, getEnvironment())); // Configure the bean factory with context callbacks. beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); ........... 

When spring initializes the bean in AbstractAutowireCapableBeanFactory , in the initializeBean method, call applyBeanPostProcessorsBeforeInitialization to implement the post bean process. this process involves injection of applicationContext.

 @Override public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName) throws BeansException { Object result = existingBean; for (BeanPostProcessor beanProcessor : getBeanPostProcessors()) { result = beanProcessor.postProcessBeforeInitialization(result, beanName); if (result == null) { return result; } } return result; } 

when a BeanPostProcessor implements an Object to execute a postProcessBeforeInitialization method, such as ApplicationContextAwareProcessor , which was added earlier.

 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( new EmbeddedValueResolver(this.applicationContext.getBeanFactory())); } 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); } } } 
+4
Nov 30 '16 at 15:16
source share

The ApplicationContextAware interface, the current application context through which you can call spring container services. We can get the current applicationContext instance introduced below by the method in the class

  public void setApplicationContext(ApplicationContext context) throws BeansException. 

To better understand how to use ApplicationContextAware, refer to this http://www.javapointer.com/spring/spring-core/how-to-use-applicationcontextaware-in-spring/

0
Jul 29 '16 at 7:52
source share



All Articles