ApplicationContextAware Interface description
Interface to be implemented by any object that wishes to be notified of the {@link ApplicationContext} that it runs in.
Copy the code
- The interface that any object that expects to get an instance of the Spring-managed ApplicationContext implements
ApplicationContext interface
/**
* Central interface to provide configuration for an application.
* This is read-only while the application is running, but may be
* reloaded if the implementation supports this.
*
* <p>An ApplicationContext provides:
* <ul>
* <li>Bean factory methods for accessing application components.
* Inherited from {@linkorg.springframework.beans.factory.ListableBeanFactory}. * <li>The ability to load file resources in a generic fashion. * Inherited from the {@link org.springframework.core.io.ResourceLoader} interface.
* <li>The ability to publish events to registered listeners.
* Inherited from the {@link ApplicationEventPublisher} interface.
* <li>The ability to resolve messages, supporting internationalization.
* Inherited from the {@link MessageSource} interface.
* <li>Inheritance from a parent context. Definitions in a descendant context
* will always take priority. This means, for example, that a single parent
* context can be used by an entire web application, while each servlet has
* its own child context that is independent of that of any other servlet.
* </ul>
*
* <p>In addition to standard {@link org.springframework.beans.factory.BeanFactory}
* lifecycle capabilities, ApplicationContext implementations detect and invoke
* {@link ApplicationContextAware} beans as well as {@link ResourceLoaderAware},
* {@link ApplicationEventPublisherAware} and {@link MessageSourceAware} beans.
*
* @author Rod Johnson
* @author Juergen Hoeller
* @see ConfigurableApplicationContext
* @see org.springframework.beans.factory.BeanFactory
* @see org.springframework.core.io.ResourceLoader
*/
public interface ApplicationContext extends EnvironmentCapable.ListableBeanFactory.HierarchicalBeanFactory.MessageSource.ApplicationEventPublisher.ResourcePatternResolver
Copy the code
- A central interface that provides configuration for the application. It is read-only while the application is running, but can be reloaded if the implementation supports it
- ApplicationContext offer:
- Bean factory methods for accessing application components. Inherited from {@ link org. Springframework. Beans. Factory. ListableBeanFactory}
- The ability to load file resources in a common manner. Inherited from {@ link org. Springframework. Core. IO. ResourceLoader} interface
- The ability to publish events to registered listeners. Inherited from {@ link ApplicationEventPublisher} interface.
- The ability to resolve messages to support internationalization. Inherits from the {@link MessageSource} interface.
- Inherits from the parent context. Definitions in descendant contexts will always take precedence. This means, for example, that the entire Web application can use a single parent context, and that each servlet has its own subcontext, independent of the subcontext of any other servlet
- In addition to standard {@ link org. Springframework. Beans. Factory. The BeanFactory} function of life cycle, The ApplicationContext implementation also detects and calls the {@Link ApplicationContextAware} bean and {@Link ResourceLoaderAware}, {@ link ApplicationEventPublisherAware} and {@ link MessageSourceAware} bean
Application scenarios
- In some Converter classes, you need to get a spring-managed bean in a static method. Since the Converter class is not spring-managed, there is no way to inject dependencies via @Autowired, @Resource. Get through ApplicationContext. GetBean method
- Annotations such as @cacheable or @Transactional do not work if the object’s methods are called internally (that is, by this reference), invalidating the proxy. Through ApplicationContext getBean method to obtain, and then call the corresponding method, can make effective agent
Code – Gets the bean’s utility class
- Implements the ApplicationContextAware interface, holds an instance of the ApplicationContext object internally, and provides the ability to get beans externally through static methods
- The code is as follows:
@Component
public class SpringContext implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContext.applicationContext = applicationContext;
}
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}
public static <T> T getBean(Class<T> clazz) {
try {
return applicationContext.getBean(clazz);
} catch (RuntimeException e) {
return null; }}/** * Gets the currently active environment **@return* /
public static String getActiveProfile(a) {
return applicationContext.getEnvironment().getActiveProfiles()[0]; }}Copy the code