Spring Configuration Bean Method (factory method and Factorybean)

Source: Internet
Author: User

Configuring Beans Through Factory methods

  

To create a bean by calling the static factory method
Creating a bean through a static factory method encapsulates the process of creating an object into a static method. When a client needs an object, it simply calls the static method without caring about the details of the object being created.
To declare a bean created through a static method, you need to specify the class of the method that owns the factory in the Bean's class attribute, notifying you that the name of the factory method is specified in the Factory-method attribute.

Finally, use the <constructor-arg> element to pass method parameters for the method
To create a bean by invoking an instance factory method
Instance Factory method: encapsulates the creation of an object into a method of another object instance. When a client needs to request an object, it simply calls the instance method without worrying about the object's creation details.
To declare a bean created through an instance factory method:
-Specify the bean that owns the factory method in the Bean's Factory-bean attribute
-Specify the name of the factory method in the Factory-method attribute
-Pass method parameters for factory methods using the Constructor-arg element

Static Factory class:

 

1  Packagecom.yl.factory;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 6 /**7 * Static Factory method: A static method that invokes a class directly can be associated with an instance of the returned bean8  * @authorYul9  *Ten  */ One  Public classStaticcarfactory { A      -     Private StaticMap<string, car> cars =NewHashmap<string, car>(); -      the     Static { -Cars.put ("Audi",NewCar ("Audi", 300000)); -Cars.put ("Ford",NewCar ("Ford", 300000)); -     } +     /** - * Static Factory method +      * @paramname A      * @return at      */ -      Public StaticCar Getcar (String name) { -          -         returncars.get (name); -     } -}

Instance Factory class:

1  Packagecom.yl.factory;2 3 ImportJava.util.HashMap;4 ImportJava.util.Map;5 /***6 * Instance Factory method: the instance factory method, which now needs to create the factory itself, calls the factory instance method to return the instance of the bean7  * @authorYul8  *9  */Ten  Public classInstancecarfactory { One      A     PrivateMap<string, car> cars =NewHashmap<string, car>(); -      -      Publicinstancecarfactory () { theCars =NewHashmap<string, car>(); -Cars.put ("Audi",NewCar ("Audi", 300000)); -Cars.put ("Ford",NewCar ("Ford", 400000)); -     } +      -      PublicCar Getcar (String brand) { +         returnCars.get (brand); A     } at}

Configuration file for the factory method:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd ">5 6     <!--Configure the Bean with a static factory method, note that instead of configuring a static factory method instance, configure the Bean instance -7     <!-- 8 Class property: A full-class name that points to a static factory method9 Factory-method: Name pointing to the static factory methodTen Constructor-arg: If the static Factory method requires an incoming parameter, use CONSTRUCTOR-ARG to configure the parameter One       - A     <BeanID= "Car1"  - class= "Com.yl.factory.StaticCarFactory" - Factory-method= "Getcar"> the         <Constructor-argvalue= "Audi"></Constructor-arg> -     </Bean> -      -     <!--Configure an instance of the factory - +     <BeanID= "Carfactory"class= "Com.yl.factory.InstanceCarFactory"></Bean> -      +     <!--  A Factory-bean: Bean that points to the instance factory method at Factory-method: Name pointing to the instance factory method - Constructor-arg: If the instance factory method requires an incoming parameter, use CONSTRUCTOR-ARG to configure the parameter -       - -     <!--to configure a bean through an instance factory method - -     <BeanID= "Car2"Factory-bean= "Carfactory"Factory-method= "Getcar"> -         <Constructor-argvalue= "Ford"></Constructor-arg> in     </Bean> - </Beans>

Configuring Beans with Factorybean

The implementation class that inherits Factorybean:

1  PackageCom.yl.factorybean;2 3 ImportOrg.springframework.beans.factory.FactoryBean;4 //custom Factorybean requires implementation of Factorybean interface5  Public classCarfactorybeanImplementsFactorybean<car> {6     7     PrivateString brand;8     9      Public voidSetbrand (String brand) {Ten          This. Brand =brand; One     } A      -     /** - * Returns the Bean object the      */ - @Override -      PublicCar GetObject ()throwsException { -         //TODO auto-generated Method Stub +         return NewCar ("BMW", 600000); -     } +     /** A * Returns the type of Bean at      */ - @Override -      PublicClass<?>Getobjecttype () { -         //TODO auto-generated Method Stub -         returnCar.class; -     } in      - @Override to      Public BooleanIssingleton () { +         //TODO auto-generated Method Stub -         return true; the     } *      $}

Configuration file:

1 <?XML version= "1.0" encoding= "UTF-8"?>2 <Beansxmlns= "Http://www.springframework.org/schema/beans"3 Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"4 xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" >5     <!-- 6 to configure an instance of a bean through Factorybean7 Class : A full-category name pointing to Factorybean8 Property: Configure the properties of the Factorybean9         Ten but the actual returned instance is the instance returned by the Factorybean GetObject () method . One       - A     <BeanID= "Car"class= "Com.yl.factorybean.CarFactoryBean"> -         < Propertyname= "Brand"value= "BMW"></ Property> -     </Bean> the </Beans>

Spring Configuration Bean Method (factory method 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.