Spring's Factory-bean & Factory-method

Source: Internet
Author: User

The two often come together, or they are often used together. But in fact it is divided into two situations:

1 simultaneous use of Factory-bean and Factory-method

If we configure Factory-bean and factory-method on a bean element at the same time, then it means that the bean is created using the Factory mode, the creator of the factory pattern, that is, the principal bean is the Factory-method Point to the bean, and the factory method? Is Factory-method. However, there is a requirement that the Factory-method method of the factory class must be non-static and must return the instance object of the current bean. But it can be a private method.

This is actually very well understood:

Factory-method method must be non-static -if it is static, then we do not need superfluous reference Factory-bean instance, and then the static factory method, do not need spring to manage it! Direct xxxfactory.getyyy (), such a way is not good?

If it is static, an exception occurs:

ExceptioninchThread"Main"Org.springframework.beans.factory.BeanCreationException:Error Creating Bean with Name'boss'Definedinch classPath resource [Beans.xml]: No matching factory method Found:factory Bean'Mans'; Factory method'FMM (String)'. Check that a method with the specified name and arguments exists and that it isnon-Static. At Org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod ( Constructorresolver.java:549) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod ( Abstractautowirecapablebeanfactory.java:1134) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance ( Abstractautowirecapablebeanfactory.java:1028) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean ( Abstractautowirecapablebeanfactory.java:513) at Org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean ( Abstractautowirecapablebeanfactory.java:483) at org.springframework.beans.factory.support.abstractbeanfactory$1. GetObject (Abstractbeanfactory.java:306) at Org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton ( Defaultsingletonbeanregistry.java: the) at Org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean (Abstractbeanfactory.java:302) at Org.springframework.beans.factory.support.AbstractBeanFactory.getBean (Abstractbeanfactory.java:197) at Org.springframework.context.support.AbstractApplicationContext.getBean (Abstractapplicationcontext.java: 1081) at Annoioctest.main (Annoioctest.java:9) at Sun.reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at Sun.reflect.NativeMethodAccessorImpl.invoke (Nativemethodaccessorimpl.java: $) at Sun.reflect.DelegatingMethodAccessorImpl.invoke (Delegatingmethodaccessorimpl.java: +) at Java.lang.reflect.Method.invoke (Method.java:601) at Com.intellij.rt.execution.application.AppMain.main (Appmain.java:144)

You have to return the instance object of the current bean--what kind of factory method would it be if you didn't return a bean instance?

Also, if we configure both Factory-bean and Factory-method on a bean element, then the bean's class attribute can be omitted, so what is the bean type? Factory-method return what is what, return String, int is also possible!

In addition, the factory method can have parameters, if there are parameters, then we need to be configured through the child element constructor-arg of the bean element . The principle is the same as the normal method of construction. However, this constructor-arg is called by Factory-bean and injected, not by the spring IoC injection.

<bean id="boss"Init-method="Initboss"lazy-init="true"Destroy-method="Destro"Factory-method="FMM"Factory-bean="Mans"> <constructor-arg value="6"></constructor-arg> </bean> <bean id="Mans" class="Com.baobaotao.Man"dependency-check="None"Scope="prototype"lazy-init="true"/>

2 static methods using the current class alone: Factory-method. That is, only the Factory-method is configured, not configured Factory-bean

These two situations are very different!!

If Factory-method is used alone, then this factory-method must be a static method of the current class. Imagine, if not static methods, but also use the factory method, but also through the spring to create beans, then it is too contradictory!

In the front we said "direct xxxfactory.getyyy (), this way is not good", in fact, this statement is not very accurate.  so at this time, what is the meaning of using the Factory-method static method alone?  I think, presumably, we can inject some parameters into the static factory method to construct our bean ! This is where spring's flexibility is.

Spring's Factory-bean & Factory-method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.