Spring is a lightweight bean container solution. The core solution is bean creation and bean dependency injection.
There are three methods for constructing bean instances in spring.(1) construct an instance through the constructor. (2) construct using static factory methods
Bean instance. (3) construct a bean instance using the instance factory method. In most cases,Spring adopts the new method (constructor method)
Construct the bean instance.In rare cases, the bean instance is constructed using static factory methods or instance factory methods. This article
This article mainly comments on the three methods for constructing instances, hoping to use them as an example:
(1) construct a bean instance through constructor:
This method is a method advocated in spring to construct bean instances. This method may require the class of the constructed instance to provide an empty
Constructor. An example of constructing an instance in xmlbeanfactory is as follows:
<Bean id = "examplebean" class = "examples. examplebean"/>
<Bean name = "anotherexample" class = "examples. examplebeantwo"/>
(2) construct bean instances using static factory methods:
When a bean instance is constructed using the static factory method, the class attribute includes the static factory method class, and the property factory-Method
The static factory method name of the created instance. The constructed instance is not necessarily an instance of the class specified by the class attribute. It can be an instance of other classes.
Spring automatically calls the static factory method when constructing an instance. The following is an example of bean definition.
<Bean id = "product1" class = "Beans. Factory" factory-method = "getproduct">
</Bean>
In the above example, the class containing the static factory method is beans. factory, and the factory method is getproduct. Spring will
By calling getproduct in beans. Factory to construct product1, this method can be used to call legacy Bean (legacy)Code
Method in
(3) construct the bean instance using the instance factory method:
This method is similar to constructing an instance using the static factory method. By calling a bean (the Bean class contains the factory used to create an instance)
Method. Note that the Bean class to be constructed does not need class in the configuration file.
But the factory-bean attribute must be used to specify the factory Bean used to create the bean. Use the factory-method attribute to specify the instance factory.
Method. The following is an example:
<! -- Bean definition that contains the instance factory method -->
<Bean id = "Factory" class = "Beans. instancefactory">
</Bean>
<! -- The bean definition that needs to be constructed through the above bean instance factory method -->
<Bean id = "product2" factory-bean = "Factory" factory-method = "createinstance">
</Bean>
The above three methods to construct bean instances,Spring advocates the first method. The following two methods can be used to construct the bean instance in the legacy code.
.