Spring Configuration Bean Method (factory method and Factorybean) "Go"

Source: Internet
Author: User
Tags instance method

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 package com.yl.factory; 2  3 import java.util.HashMap; 4 import java.util.Map; 5  6/** 7  * Static Factory method: A static method that calls a class directly can be associated with an instance of the returned Bean 8  * @a  Uthor Yul 9  *10  */11 public class Staticcarfactory {$     private static map<string, car> cars = new Hashmap<string, car> ();     //static blocks are used to create a map object, and then use map to get the specified Car object from     static {         cars.put ("Audi") according to key. New car ("Audi", 300000)),         cars.put ("Ford", New Car ("Ford", 300000)),     }19     /**20      * Static Factory method 21      * @param name22      * @return23      */24 public     static Car Getcar (String name) {+         -         return Cars.get (name);     }28}

Instance Factory class:

1 package com.yl.factory; 2  3 import java.util.HashMap; 4 import java.util.Map; 5/*** 6  * Instance Factory Method: Instance factory method, which is now required to create the factory itself, in the call factory instance method to return the instance of the bean  7  * @author Yul 8  * 9  */10 public class Instancecarfactory {     map<string, car> cars = new hashmap<string, car> ();     initializes the map object via the default constructor.     instancecarfactory ()         Hashmap<string, Car> (),         cars.put ("Audi", New car ("Audi", 300000)),         cars.put ("Ford", New Car (" Ford ", 400000));     }19 public     Car Getcar (String brand) {         cars.get (brand);     }23 }

Configuration file for the factory method:

 1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns= "Http://www.springframework.org/schema/beans" 3 xmln S: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 the static factory method instance is not configured, Instead, configure the bean instance--7 <!--8 class properties: A full class name pointing to the static factory method 9 Factory-method: The name of the static Factory method constructor -arg: If the static Factory method requires an incoming parameter, use CONSTRUCTOR-ARG to configure the parameter one by one-->12 <bean id= "Car1" class=      Carfactory "factory-method=" Getcar ">15 <constructor-arg value=" Audi "></constructor-arg>16 </bean>17 <!--configuration Factory instance-->19 <bean id= "carfactory" class= "Com.yl.factory.InstanceCarFac         Tory "></bean>20 <!--Factory-bean: bean23 Factory-method to the instance factory method: Name of the instance factory method 24 Constructor-arg: If the instance factory method needs to be passedparameters, use Constructor-arg to configure the parameters-->26 <!--Configure the bean-->27 <bean id= "car2" factory-bean= "Carf" through the instance factory method Actory "factory-method=" Getcar ">28 <constructor-arg value=" Ford "></constructor-arg>29 </bean >30 </beans>

Configuring Beans with Factorybean

The implementation class that inherits Factorybean:

1 package Com.yl.factorybean; 2  3 import Org.springframework.beans.factory.FactoryBean; 4//Custom Factorybean need to implement Factorybean interface 5 public class Carfactorybean implements factorybean<car> {6      7     private String brand; 8      9 public     void Setbrand ( String brand) {         This.brand = brand;11     }12     /**14      * Returns the object of the bean      */16     @Override17 Public     Car GetObject () throws Exception {         //TODO auto-generated method stub19         return new Car ("BMW", 600 )     }21     /**22      * Returns the type of Bean      */24     @Override25 public     class<?> Getobjecttype () {+         //TODO auto-generated method Stub27         return car.class;28     }29     Override31 Public     Boolean Issingleton () {+         //TODO auto-generated method stub33         return true;34     }     36}

Configuration file:

1 <?xml version= "1.0" encoding= "UTF-8"?> 2 <beans xmlns= "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 by using Factorybean 7         class: A full class name pointing to Factorybean 8         property: Configuring Factorybean Properties 9         But the actual instance returned is the instance returned by the Factorybean GetObject () method      -->12     <bean id= "car" class= " Com.yl.factorybean.CarFactoryBean ">13         <property name=" brand "value=" BMW "></property>14     </bean>15 </beans>

Spring Configuration Bean Method (factory method and Factorybean) "Go"

Related Article

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.