Spring factory method creates (instantiates) bean instance code
Clear objectives
Briefly describe the problem to be solved in this article: how to transfer the bean creation process to developers instead of using spring to create bean instances in spring.
have a lucid brain
How to create a bean instance:
1) By constructor (with or without parameters)
Method: < bean id = "" class = "" / >
2) Through static factory method
Method: < bean id = "" class = "factory class" factory method = "static factory method" / >
Note: the factory class instance was not created
3) By instance factory method (non static method)
Method:
< bean id = "factory" class = "factory class" / >
< bean id = "" factory bean = "factory" factory method = "instance factory method" / >
Note: factory class instances are created
Practical method
Example 1:
Requirements:
I don't want any more beans The bean is instantiated when XML is loaded, but the loaded bean XML is separated from instantiated objects.
2. Implement the bean of the singleton
In the above cases, you can create beans through the factory method factory method.
Then load the bean XML, the bean will not be instantiated directly, but the real instantiation will start when the method referred to in factory method is called.
Implementation: create a singleton bean through spring's factory method. First, create a singleton object through a static internal class
Specify the loaded method getInstance in the spring configuration file
Call the bean to get the instance through the application context
results of enforcement
Introduction to creating beans using factory methods
1. Create bean using static factory method
When using the static factory method to create a bean instance, the class attribute must also be specified, but at this time, the class attribute does not specify the implementation class of the bean instance, but the static factory class. Because spring needs to know which factory is used to create bean instances. In addition, you need to use factory method to specify the static factory method name, Spring will call the static factory method (may contain a set of parameters) to return a bean instance. Once the specified bean instance is obtained, the processing steps later in spring are exactly the same as creating a bean instance using the normal method. It should be noted that when using the static factory method to create a bean, the factory method must be static. This description sounds a little dizzy. I won't say much. The code is as follows:
First define an interface, and the static method will generate an instance of the interface:
The following are two implementation classes of the interface:
The following animalfactory factory contains a static method of getanimal, which will determine which object to create according to the passed in parameters. This is a typical static factory design pattern.
If you need to specify that spring uses animalfactory to generate animal objects, you can make the following configuration in the spring configuration file:
From the above configuration, we can see that the class and factory method configured by cat and dog beans are exactly the same, because both instances are generated by using the same static factory class and the same static factory method. Only the parameters specified for this static factory method are different. Use the < constructor Arg / > element to specify parameters for the static factory method.
The main program's method of obtaining cat and dog bean instances remains unchanged. Similarly, it only needs to call getBean () of spring container:
Output results:
When creating an instance using a static factory method, you must provide the factory class and the static factory method that generates the instance. When creating an instance through the static factory method, you need to make the following changes to the spring configuration file;
The class attribute is no longer the implementation class of the bean instance, but the static factory class that generates the bean instance
Use factory method to specify the static factory method of the production bean instance
If a static factory method requires parameters, configure it with the < constructor Arg / > element
When we specify that spring uses the static factory method to create a bean instance, spring will first parse the configuration file, call the static factory method of the static factory class through reflection according to the information specified in the configuration file, and take the return value of the static factory method as a bean instance. In this process, spring is no longer responsible for creating a bean instance, Bean instances are provided by user provided static factory methods.
2. Use the instance factory method to create a bean
There is only one difference between the instance factory method and the static factory method: calling the static factory method only needs to use the factory class, and calling the instance factory method must use the factory instance. Therefore, there is only one difference in spring configuration: the configuration static factory method specifies the static factory class, and the configuration instance factory method specifies the factory instance. The same example above modifies animalfactory to:
The spring file is modified to:
The test class does not need to be modified, and the output result is the same as above.
In many cases, < bean id = "bean1" class = "..." / > is used to define a bean. In this way, spring will call the default parameterless construction method to create a bean instance. In addition, you can also use the factory method to create bean instances, realize the separation of bean creation and use, and hand over the bean creation to the factory.
There are three ways to configure factory beans.
Abstract interface:
1. Get bean instance by static factory method
Factory type:
Profile:
Test class:
2. Obtain the bean instance by using the method of the factory instance
Factory type:
Profile:
The "factory bean" attribute specifies the factory bean, and the "factory method" attribute specifies the factory method to obtain the bean instance.
Test class:
3. The factory class implements org springframework. beans. factory. Factorybean interface
Factory type:
Profile:
Test class:
The classes that implement the factorybean interface will not be regarded as ordinary beans. Spring will automatically detect and call the GetObject method to obtain the bean instance
summary
That's all for the spring factory method instantiation bean instance. You can leave a message to point out the shortcomings. Thank you for your support!