The factory bean here is different from the previous example factory method to create bean, or the static factory method to create Bean Factory: the previous factories are standard factory models, spring is only responsible for calling factory methods to create bean instances.Factory BeanIs a special bean of spring. This factory Bean must be implementedFactorybean Interface.
Factorybean interface is the standard interface of factory bean. bean that implements this interface can only be used as factory bean. When we deploy factory bean in a container and use getbean () the container does not return the factorybean instance, but returns the product of factorybean.
Factorybean InterfaceThe following three methods are provided:
Object GetObject () |
This method is used to return the Java instance generated by the factory bean. |
Class getobjecttype () |
This method returns the implementation class of the Java instance generated by the factory bean. |
Boolean issingleton () |
This method indicates whether the Java instance generated by bean in the factory is in singleton mode. |
The bean implementing the factorybean interface cannot be used as a normal bean. configuring factorybean is no different from configuring a common bean. However, when the client requests the bean ID, the container returns the factorybean product, instead of returning the factorybean itself.
Person. Java:
public interface Person {public String sayHello(String name);public String sayGoodBye(String name);}
Chinese. Java:
Public class Chinese implements person {@ overridepublic string saygoodbye (string name) {return "goodbye," + name ;}@ overridepublic string sayhello (string name) {return "hello, "+ name ;}}
Personfactory. Java:
Public class personfactory implements factorybean {private person P; // return the public person GetObject () {If (P = NULL) product produced by the factory bean) {P = new Chinese () ;}return P ;}// obtain the public class of the product produced by the factory bean. <?> Getobjecttype () {return Chinese. Class;} // returns whether the product produced by the factory Bean is a singleton public Boolean issingleton () {return true ;}}
Bean. XML Core Configuration:
<bean id="chinese" class="com.bean.PersonFactory"/>
Test. Java:
Public class test {public static void main (string [] ARGs) {applicationcontext CTX = new classpathxmlapplicationcontext ("bean. XML "); // directly request factorybean, the system returns the product person p1 = (person) CTX of this factorybean. getbean ("Chinese"); // 1system. out. println (p1.sayhello ("Tom"); system. out. println (p1.saygoodbye ("Tom"); // obtain the factorybean product person P2 = (person) CTX again. getbean ("Chinese"); // 2system. out. println (p1 = P2); system. out. println (CTX. getbean ("& Chinese"); // 3 }}
Run test. Java and the console outputs:
The above personfactory is a standard factory bean. the GetObject () method of the personfactory ensures that the person object generated every time is a singleton object. Therefore, the issingleton () method of the factory class returns true.
Test. in Java, 1 and 2 directly request the factorybean in the container. Spring will not return the factorybean instance, but will return the product of the factorybean. In Program 3, add the & symbol before the bean ID, this will allow spring to return factorybean itself.
BecauseFactorybean manages product beans in a singleton ModeTherefore, the two requested products are the same shared instance.
When the program needs to obtain the factorybean itself, it does not directly request the bean ID, but adds the & symbol before the bean ID.
It may be hard for beginners to understandFactory Bean. In fact, factorybean is a very useful interface in spring. Spring provides many practical factory beans, such as transactionproxyfactorybean. This factory bean is used to create a transaction proxy for the target bean.
Most factory beans provided by spring end with the suffix of factorybean. Most factory beans provided by spring are used to produce a batch of bean instances with certain characteristics. Factory beans are an important tool class of spring.