Explain in detail the life cycle, scope and implementation of beans in spring
preface
In ApplicationContext After configuring beans in XML, what are the declaration Cycle States of beans. What can be done at each stage of the life cycle. In ApplicationContext What are the scopes of XML configuration beans. What does each scope represent. For what situation. Make a record of this article.
life cycle
initialization
You can directly view the pictures from the spring bean life cycle
As can be seen from the above figure, bean initialization includes 9 steps. Some of these steps include the implementation of the interface, including beannameaware interface and beanfactoryaware interface. Applicationcontextaware interface. Beanpostprocessor interface, initializingbean interface. So what role do these interfaces play throughout the life cycle? We will introduce them one by one later.
Before instantiation
When all the attributes of a bean are set, it is often necessary to perform some specific behaviors. Spring provides two ways to achieve this function:
Specify initialization method
As follows:
Write loader
Configure bean
Note the init method parameter
results of enforcement
Implement the initializingbean interface
Implementing the initializingbean interface will implement the afterpropertiesset method, which will be called automatically. But this approach is invasive. Generally, it is not recommended to use.
Implement the afterpropertiesset method
Configuration XML
result:
Destroy
Similarly, the above figure shows the process of bean destruction. Includes the disposablebean interface.
Using the destroy method method
Configure bean
Configure loader
result:
Implement the disposablebean interface
Implement the disposablebean interface
configuration file
Scope of spring bean
Configuration example
Use method injection to coordinate beans with different scopes
Normally, if the singleton scope depends on the singleton scope. That is, the same object is obtained every time. Similarly, the prototype scope depends on the prototype scope, and new objects are obtained every time. However, if singleton depends on the prototype scope, the prototype in singleton is created for the first time every time. How to coordinate this relationship. Make sure what you get is correct every time.
In this case, spring provides a lookup method to solve this problem.
First, we define a prototype:
Then inject through the interface:
Configure a single instance:
Profile:
Load profile:
result:
As can be seen from the result diagram above, the objects generated in the previous way are the same every time. Injection through lookup is different each time. Can solve this problem. But is there a simpler way to feel the advantages and troubles of this way.
Make bean aware of spring container
Implement beannameaware and set the ID value by yourself.
Implement beanfactoryaware and applicationcontextaware aware aware spring container. Get the spring container.
Spring internationalization support
Configuration profile
New Chinese profile
message_ zh_ CN. properties:
New English profile
message_ en_ US. properties:
Load profile
result
summary
Well, the above is the whole content of this article. I hope the content of this article can bring some help to your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.