Preface
In some cases, when a bean needs to implement a certain function, it is necessary to use theSpring container object properties
Only then can it be achieved. At this time, you need to ask the bean to first obtain the Spring container object attributes, and then implement the function with the Spring container object attributes.
- In order for the bean to obtain the Spring container object properties where it is located, the bean can be implemented such as the ApplicationContextAware interface.
- By implementing the ApplicationContextAware interface and overriding the set method, the Spring container will automatically call the setApplicationContext method in the ApplicationContextAware interface by the context object, and then you can obtain the bean in the Spring container through the ApplicationContext object.
- Aware can be understood as attribute injection, but the difference is that only when the bean that implements this interface will automatically inject the ApplicationContext when the spring container is initialized.
Aware
The Aware interface is an empty interface, which can be understood as a marking interface, which is convenientIn a unified method () judgment processing assignment, write out their respective set methods on the subinterface.
**The main function of the Aware interface method is to enable beans to obtain relevant resources and information of Spring containers for use in subsequent initialization and business logic. **It can be understood that the previous attribute assignment is to assign the value of our custom attribute, and using Aware allows us to get the container object inside the Spring container.
/* * A marker superinterface * The interface is also explained in the comments of this interface as a tagged interface */ public interface Aware { }
Common Aware interfaces and functions
BeanNameAware interface
- When a Bean implements the BeanNameAware interface, it can obtain its own Bean name in the Spring container. There is only one method in this interface setBeanName(String name). The Spring container will call this method during the creation of the bean and pass the name of the bean as a parameter.
ApplicationContextAware interface
- The bean implementing this interface can obtain Spring's application context (ApplicationContext) object. ApplicationContext is the core interface of Spring container. It provides access to all beans in the container, and includes various functions such as obtaining resources and publishing events.
BeanFactoryAware interface
- Similar to ApplicationContextAware, beans that implement the BeanFactoryAware interface can obtain Bean factory (BeanFactory) objects. BeanFactory is the basic interface used in Spring containers to create and manage beans, which is more lightweight than ApplicationContext.
ApplicationEventPublisherAware interface
- If the bean needs to publish application events, this interface can be implemented.
ResourceLoaderAware interface
- If the bean needs to load resource files (such as XML files, attribute files, etc.), this interface can be implemented.
EnvironmentAware interface
- This interface can be implemented if the bean needs to access environment-specific information (such as configuration file location, activated configuration file, etc.).
MessageSourceAware interface
- If the bean needs to be processed internationally, this interface can be implemented to obtain the message source.
Code Example
For example: Bean implements the ApplicationContextAware interface. At this time, Spring will assign a value to the ApplicationContext property value in the Bean through setApplicationContext().
public interface ApplicationContextAware extends Aware { void setApplicationContext(ApplicationContext applicationContext) throws BeansException; }
Implement the ApplicationContextAware interface through a class and override the setApplicationContext() method in the interface. You can get the application context ApplicationContext object of Spring
public class SpringContextUtil implements ApplicationContextAware { private ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { = applicationContext; } public ApplicationContext getApplicationContext() { return applicationContext; } }
The call time of set() in the Aware interface
In the above code, the setApplicationContext() method is called by the spring container and is called uniformly in invokeAwareMethods.
When the sub-implementation class implements the ApplicationContextAware interface, the setApplicationContext method in the current class is overridden as the setApplicationContext method in the interface. Spring will assign values by the container when executing invokeAwareMethods().
The specific implementation in Spring source code is as follows:
#invokeAwareMethods
private void invokeAwareMethods(String beanName, Object bean) { //If bean is an Aware instance if (bean instanceof Aware) { //If the bean is a BeanNameAware instance if (bean instanceof BeanNameAware) { //Calling the setBeanName method of bean ((BeanNameAware) bean).setBeanName(beanName); } //If the bean is a BeanClassLoaderAware instance if (bean instanceof BeanClassLoaderAware) { //Get the class loader of this factory to load the Bean class (even if the system classLoader cannot be used, it can only be null) ClassLoader bcl = getBeanClassLoader(); if (bcl != null) { //Calling the setBeanClassLoader method of bean ((BeanClassLoaderAware) bean).setBeanClassLoader(bcl); } } //Check whether the bean implements BeanFactoryAware. If so, call its setBeanFactory method to set the factory instance. if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(); } } }
When to call the invokeAwareMethods method
invokeAwareMethods() is usually called during the Spring container's initialization of a bean.
- Specifically, after the bean is created and configured, but not fully initialized (i.e. before the init-method or InitializingBean's afterPropertiesSet() method is called), the Spring framework will check whether the bean implements certain specific interfaces, such as BeanNameAware, BeanFactoryAware, etc., and call the methods of these interfaces accordingly to set necessary properties.
- This process is part of the Spring framework, which is used to ensure that the beans can obtain some information about themselves or Spring application context.
- For example, let the bean know its name, its BeanFactory, or its class loader. This gives the bean more context information early in the life cycle of the bean.
This is the end of this article about understanding the Spring Aware interface. For more related Spring Aware interface content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!