Beanfactory vs 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 comply with, that is, the minimum and most basic programming specifications that Spring IoC complies. In spring code, beanfactory is only an interface, not a specific implementation of the IOC container, but the spring container provides many implementations, such as defalistlistablebeanfactory, xmlbeanfactory, and applicationcontext, all are implemented with some function attached.

 

Java code
  1. Package org. springframework. Beans. factory;
  2. Import org. springframework. Beans. beansexception;
  3. Public interface beanfactory {
  4. String factory_bean_prefix = "&";
  5. Object getbean (string name) throws beansexception;
  6. <T> T getbean (string name, class <t> requiredtype) throws beansexception;
  7. <T> T getbean (class <t> requiredtype) throws beansexception;
  8. Object getbean (string name, object... ARGs) throws beansexception;
  9. Boolean containsbean (string name );
  10. Boolean issingleton (string name) throws nosuchbeandefinitionexception;
  11. Boolean isprototype (string name) throws nosuchbeandefinitionexception;
  12. Boolean istypematch (string name, class <?> Targettype) throws nosuchbeandefinitionexception;
  13. Class <?> GetType (string name) throws nosuchbeandefinitionexception;
  14. String [] getaliases (string name );
  15. }

 

 

2. factorybean

In general, spring uses the reflection mechanism to specify the class to implement bean instantiation using the class attribute of <bean>. In some cases, the bean instantiation process is complicated, you need to provide a large amount of configuration information in <bean>. The flexibility of the configuration method is limited. In this case, the encoding method may obtain a simple solution. Spring provides a factory interface for org. springframework. Bean. Factory. factorybean. You can customize the logic of bean instantiation by implementing this interface.

Factorybean interfaces occupy an important position in the Spring framework. spring itself provides more than 70 factorybean implementations. They hide the details of instantiating some complex beans and facilitate upper-layer applications. Since spring 3.0, factorybean supports generics, that is, the interface declaration is changed to the form of factorybean <t>:

 

Java code
  1. Package org. springframework. Beans. factory;
  2. Public interface factorybean <t> {
  3. T GetObject () throws exception;
  4. Class <?> Getobjecttype ();
  5. Boolean issingleton ();
  6. }

 

This interface also defines the following three methods:

T GetObject (): return the bean instance created by factorybean. If issingleton () returns true, the instance will be placed in the cache pool of a single instance in the spring container;

Boolean issingleton (): returns whether the scope of the bean instance created by factorybean is Singleton or prototype;

Class <t> getobjecttype (): return the bean type created by factorybean.

When the implementation class of the <bean> class attribute configuration in the configuration file is factorybean, The getbean () method returns not the factorybean itself, but the object returned by the factorybean # GetObject () method, it is equivalent to factorybean # GetObject () proxy for the getbean () method.

For example, if you configure the <bean> of the car in the traditional way, each attribute of the car corresponds to a <property> element tag.

 

Java code
  1. Package com. baobaotao. factorybean;
  2. Public class car {
  3. Private int maxspeed;
  4. Private string brand;
  5. Private double price;
  6. Public int getmaxspeed (){
  7. Return this. maxspeed;
  8. }
  9. Public void setmaxspeed (INT maxspeed ){
  10. This. maxspeed = maxspeed;
  11. }
  12. Public String getbrand (){
  13. Return this. Brand;
  14. }
  15. Public void setbrand (string brand ){
  16. This. Brand = brand;
  17. }
  18. Public double getprice (){
  19. Return this. price;
  20. }
  21. Public void setprice (double price ){
  22. This. Price = price;
  23. }
  24. }

 

If factorybean is used for implementation, it is flexible. In the following example, the configuration value is specified for all attributes of the car by using commas:

 

Java code
  1. Package com. baobaotao. factorybean;
  2. Import org. springframework. Beans. Factory. factorybean;
  3. Public class carfactorybean implements factorybean <car> {
  4. Private string carinfo;
  5. Public Car GetObject () throws exception {
  6. Car car = new car ();
  7. String [] Infos = carinfo. Split (",");
  8. Car. setbrand (Infos [0]);
  9. Car. setmaxspeed (integer. valueof (Infos [1]);
  10. Car. setprice (double. valueof (Infos [2]);
  11. Return car;
  12. }
  13. Public class <car> getobjecttype (){
  14. Return car. Class;
  15. }
  16. Public Boolean issingleton (){
  17. Return false;
  18. }
  19. Public String getcarinfo (){
  20. Return this. carinfo;
  21. }
  22. // Accept the comma delimiter to set the attribute information
  23. Public void setcarinfo (string carinfo ){
  24. This. carinfo = carinfo;
  25. }
  26. }

 

With this carfactorybean, you can use the following custom configuration method to configure the car bean in the configuration file:

<Bean id = "car" class = "com. baobaotao. factorybean. carfactorybean"

P: carinfo = "Ferrari, 400,2000000"/>

When getbean ("car") is called, spring uses the reflection mechanism to find that carfactorybean implements the factorybean interface. Then, the spring container calls the interface method carfactorybean # GetObject () to return the result. If you want to obtain the carfactorybean instance, you need to add the "&" prefix before beanname when using the getbean (beanname) method, such as getbean ("& Car ");

 

3. Differences

Beanfactory is a factory, that is, an IOC container or an object factory, and factorybean is a bean. In spring, all beans are managed by beanfactory (IOC container. However, for factorybean, this bean is not a simple bean, but a factory bean that can produce or modify objects. Its implementation is similar to the factory mode and modifier mode in the design mode.

 

Beanfactory vs 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.