Beanfactory and Factorybean

Source: Internet
Author: User

1, Beanfactory

Beanfactory defines the most basic form of the IOC container and provides the most basic interface that the IOC container should adhere to, which is the lowest and most basic programming specification that spring IOC adheres to. In spring code, Beanfactory is just an interface, not a concrete implementation of the IOC container, but the spring container gives many implementations, such as Defaultlistablebeanfactory, Xmlbeanfactory, applic Ationcontext and so on, are attached to a certain function of the implementation.

The following is the definition of the Beanfactory interface:

2, Factorybean

In general, Spring uses the class attribute of <bean> to specify the implementation class instantiation bean through the reflection mechanism, in some cases, the instantiation bean process is more complex, if in the traditional way, you need to provide a lot of configuration information in <bean> 。 The flexibility of configuration is limited, and it is possible to get a simple solution by coding. Spring provides a Org.springframework.bean.factory.FactoryBean factory class interface that allows the user to customize the logic of the instantiated bean by implementing the interface.

The Factorybean interface occupies an important place for the Spring framework, and users can customize the logic of the bean instantiation by implementing the Factory interface. Spring itself provides implementations of more than 70 factorybean. They hide the details of instantiating some complex beans, which facilitates the application of the upper layer. Starting with Spring 3.0, Factorybean began to support generics, i.e. the interface declaration was changed to factorybean<t> form:

The following 3 methods are also defined in this interface:

T getObject (): Returns the Bean instance created by Factorybean and if Issingleton () returns True, the instance is placed in a single-instance cache pool in the spring container;

Boolean Issingleton (): Returns whether the scope of the Bean instance created by Factorybean is Singleton or prototype;

Class<t> Getobjecttype (): Returns the Bean type created by Factorybean.

When the implementation class for the <bean> class property configuration in the configuration file is Factorybean, the Getbean () method is returned instead of the Factorybean itself, but Factorybean#getobject () The object returned by the Factorybean#getobject method is equivalent to the Getbean () method represented by the () proxy.

Example: If you use the traditional way to configure the <bean> of car below, each property of car corresponds to a <property> element label respectively.

If you implement it in a Factorybean way, the following example specifies the configuration value for all properties of Car at once by using the comma delimiter method:

With this carfactorybean, you can configure the Car Bean in the configuration file using the following custom configuration method:

When Getbean ("car") is called, Spring discovers that Carfactorybean implements the Factorybean interface through the reflection mechanism, when the spring container calls the interface method Carfactorybean#getobject () Method is returned. If you want to get an instance of Carfactorybean, you need to add a "&" prefix that appears before beanname when using the Getbean (Beanname) method: such as Getbean ("&car");

3. Difference

Beanfactory is a factory, which is the IOC container or object factory, the Main method is Getbean (String beanname), which returns the bean of a particular name from the container (beanfactory as an IOC containerthe top-level abstraction interface, which in theory is just a bean factory that maintains dependency relationships , is responsible for initializing the IOC container Work and provide access to the bean definition. The interface is under the Org.springframework.beans package, which represents a derived class with xmlbeanfactory )。 Factorybean is a special 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

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.