Through the following two articles:

  1. “Where did I come from” in the Spring Bean lifecycle?
  2. “Where am I going” in the Spring Bean lifecycle?Now that we know the life cycle core of Spring beans, how are beans initialized toReady for UseIs destroyed when the resource is reclaimed, butSpring Bean Life CycleThe graph is not completely lit up, so this article will light up the rest of the content and talk about your common XxxxAware interfaces

Why is the Spring Bean lifecycle also referred to as Aware? Come down and light up the rest and you might see:

  1. In the Spring BeanReady for UseThe previous origin was, of course, to invoke the Constructor, so Constructor is undoubtedly the first step in creating a Spring Bean
  2. Do Dependency Injection via Setter methods, SDI (Setter Dependency Injection)
  3. Once dependency injection is finished,BeanNameAware.setBeanName()Is called, which sets the bean’s name in the bean Factory
  4. Next callBeanClassLoaderAware.setBeanClassLoader()We know that all classes need to be loaded into the context through the class loader. We’ll give you more information about the class loading mechanism/parent delegation model later
  5. thenBeanFactoryAware.setBeanFactory()Is called to provide the bean instance with its own Factory

I’d like to say a little more about 1 and 2. Look at the following code:

Here, we try to access the auto-injected field Environment env through the constructor. When the constructor is called, the Spring Bean is not fully initialized, which causes NullPointerExceptions; Let’s switch gears:

This way, the @PostConstruct method is called after the Environment instance has been safely injected, so no NullPointerException is thrown.

Looking back at the period chart, does anything suddenly open up?

On the blackboard

It is safe to wait until all Spring beans have completed dependency injection (the Setter Methods section of the cycle diagram) before using Bean references,

Stay tuned for an upcoming section on Spring’s awkward approach to dependency injection that is often asked in interviews

Here we can finally say Aware, and look

Aware

Aware; Aware; Aware 2. be aware of “Perceived”, XxxxAware is to…. Is not Aware that you are not Aware? For after ather

The biggest advantage of Spring’s dependency injection is that all beans are unaware of the existence of the Spring container. Take the “Where did I come from” aspect of the Spring Bean lifecycle. The example of “primary school students joining the Young Pioneers” in the article shows that primary school students are still the same primary school students, and the rules for joining the Young Pioneers or the Communist Youth League are just different. However, in practical projects, we will inevitably use the resources provided by the Spring container itself (it is inevitable that we need the help of the Young Pioneers organization). In this case, the Bean must be actively Aware of the existence of the Spring container in order to invoke the resources provided by Spring. This is Spring Aware. In fact, Spring Aware is designed for internal use within the framework. If you use Spring Aware, your Bean will be coupled to the Framework, so you won’t use it alone, but I hope you won’t be confused when reading the framework source code.

Common Spring Aware interfaces

Aware subinterface describe
BeanNameAware Gets the name of the Bean in the container
BeanFactoryAware Gets the current BeanFactory so that the container’s services can be invoked
ApplicationContextAware Same as above, inBeanFactory vs. ApplicationContextIs clearly stated in
MessageSourceAware Gets the relevant text information for the Message Source
ApplicationEventPublisherAware Publish event
ResourceLoaderAware Get the resource loader to get the external resource file

Look at the class diagram:

More than that, of course, Spring Aware is often used for beans to be served by the Spring container.

Code sample

BeanNameAware

Custom bean implementation BeanNameAware

Registered bean

run

As expected, the BeanName output is myCustomBeanName, or getMyBeanName if the Name attribute of the @bean annotation is removed

conclusion

In most cases, we should avoid using any Aware interfaces unless we need them. Implementing these interfaces will couple your code to the Spring framework, but hopefully reading the framework’s source code will be clearer after reading this section

Soul asking

  1. What are the classic Aware applications in the framework?
  2. Do you have a good understanding of the Spring Bean lifecycle by now?

The Demo code

The test code related to the Spring Bean life cycle is not written here because it is too much. Please follow the public account and reply to “Demo” to get the relevant code. Please try to run the result by yourself


Efficiency tools


Welcome to continue to pay attention to the public account: “One soldier of The Japanese Arch”

  • Cutting-edge Java technology dry goods sharing
  • Summary of effective Tools
  • Interview question analysis and solution
  • Technical data collection

The next article will be a series of tutorials on ElasticSearch and Multithreading in Java

Continue to pay attention, take you like reading detective novel easy fun learning Java technology stack knowledge