Beanfactory and Factorybean in spring learning note--spring

Source: Internet
Author: User

Beanfactory is an interface under Spring org.springframework.beans.factory and is the basic programming specification that spring IOC adheres to. His realization class has defaultlistablebeanfactory, Xmlbeanfactory, ApplicationContext and so on, each realizes own function.
Usually spring uses the Bean's class attribute to specify the instantiation bean of the implementation class through the reflection mechanism, and in some cases the process of instantiating the bean is more complex, and if you need to provide a large amount of configuration information in the bean in the traditional way, Instead, Spring provides a Org.springframework.bean.factory.FactoryBean factory interface class that enables users to customize the logic of instantiating beans by implementing the interface. The following is the source code of Factorybean, comments are very powerful, but my English in general, simple translation.

/** * Interface toBe implemented by objects used within a {@link beanfactory} * which is themselves factories. If a Bean implements thisInterface, * It is used asA factory forAn object toExpose, not directly asA bean * instance that'll be exposed itself. * * <p><b>nb:a Bean that implements thisInterfaceCannot be used asA * Normal bean.</b> a factorybean is definedinchA bean style, but the * object exposed forBean references ({@link #getObject ()} is always * The object, it creates. * <p>factorybeans can support single Tons andPrototypes, andcan * either create objects lazily on demandoreagerly on startup. * The {@link Smartfactorybean}InterfaceAllows forexposing * More fine-grained behavioral metadata. * * <p>thisInterfaceis heavily used within the framework itself, for* Example forThe AOP {@link Org.springframework.aop.framework.ProxyFactoryBean} *orThe {@link Org.springframework.jndi.JndiObjectFactoryBean}. * It can be used forApplication components asWell However, * this is not common outside ofInfrastructure code. * * <p><b>NOTE:</b> Factorybean objects participateinchThe containing * beanfactory ' s synchronization ofBean creation. There is usually no * need for InternalSynchronization other than forPurposes of Lazy* Initialization within the Factorybean itself (orthe like). * * @author Rod Johnson * @author Juergen Hoeller * @since08.03. 2003* @see Org.springframework.beans.factory.BeanFactory * @see Org.springframework.aop.framework.ProxyFactoryBean * @see Org.springframework.jndi.JndiObjectFactoryBean * * Public Interfacefactorybean<t> {/** * Return an instance (possibly sharedorIndependent) ofThe object * Managed by this factory. * <p>as withA {@link beanfactory}, this allows support forBoth the * Singleton andPrototype design pattern. * <p>if This factorybean isn't fully initialized yet at the time of* The Call ( forExample because it is involvedinchA circular reference), * throw a corresponding {@link factorybeannotinitializedexception}. * <p>as ofSpring2.0, Factorybeans is allowed to return{@codeNULL} * objects. The factory would consider this asNormal value tobe used; It * would not throw a factorybeannotinitializedexceptioninchThis case anymore. * Factorybean implementations is encouraged toThrow * Factorybeannotinitializedexception themselves now, asappropriate. * @returnAn instance ofThe bean (can be {@codeNULL}) * @throws ExceptioninchCase ofCreation Errors * @see factorybeannotinitializedexception * *//Returns a bean instance with Factorybean created, and if Issingleton is true, the instance is placed in the spring containerT GetObject () throws Exception; /** * Return the type of  object   that this factorybean creates, *or{@codeNULL}ifNot knowninchAdvance. * <p>this allows one toCheck forSpecific types ofBeans without * instantiating objects, forExample on autowiring. * <p>in the case ofImplementations that is creating a singleton object, * This method shouldTry  toAvoid singleton creation asFar asPossible * It should rather estimate the type  in advance.* For prototypes, returning a meaningful type   here is advisable too.* <p>this method can called <i>before</i> this factorybean have * been fully initialized. It must not rely on the state created during * initialization; ofCourse, it can still UseSuch stateifAvailable. * <p><b>NOTE:</b> autowiring'll simply ignore Factorybeans thatreturn* {@codeNULL} here. Therefore It is highly recommended toImplement * This method properly, using the ofThe Factorybean. * @returnThe type of  object   that this factorybean creates, *or{@codeNULL}ifNot known at the time ofThe call * @see Listablebeanfactory#getbeansoftype * *//Return Factorybean Create Bean typeClass<?> Getobjecttype (); /** * is the object managed by this factory a singleton? That is, * would {@link #getObject ()} AlwaysreturnThe same object * (a reference that can is cached)? * <p><b>NOTE:</b> If a factorybean indicates toHold a singleton object, * The object returned from {@code getObject ()} might get cached * by the owning Beanfacto Ry. Hence, DoNotreturn{@codetrue} * Unless the Factorybean always exposes the same reference. * <p>the Singleton status ofThe factorybean itself would generally * be provided by the owning beanfactory; Usually, it has tobe * defined asSingleton there. * <p><b>NOTE:</b> This method returning {@codefalse} does not * necessarily indicate that returned objects is independent instances. * An implementation ofThe extended {@link Smartfactorybean}Interface* May explicitly indicate independent instances through its * {@link smartfactorybean#isprototype ()} method. Plain {@link Factorybean} * implementations which DoNot implement this extendedInterfaceis * simply assumed toAlwaysreturnIndependent instancesifThe * {@code Issingleton ()} implementation returns {@codefalse}. * @returnWhether the exposed object is a singleton * @see #getObject () * @see smartfactorybean#isprototype () *///Returns whether the scope of the bean instance created by Factorybean is singleton or prototypeBoolean Issingleton ();}

When the implementation class for the Class property configuration in the configuration file is Factorybean, the Getbean method returns the object that is not the Factorybean itself, but the Factorybean#getobject () method. Equivalent to Factorybean#getobject () agent for Getbean
The difference between the two:
Beanfactory is a factory, an IOC container or object factory, Factorybean is a bean, and in spring all beans are managed by the Beanfactory (IOC). But for Factorybean, the Bean is a factory bean that produces or modifies objects, and his implementation is similar to the factory pattern and decorator pattern in design mode.

Beanfactory and Factorybean in spring learning note--spring

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: 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.