Spring Configuration Bean method

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:

 1Package com.yl.factory;23Import Java.util.HashMap;4Import Java.util.Map;56/**7 * Static Factory method: A static method that directly invokes a class can be associated with an instance of the returned Bean 8 *@author Yul 9 *10 */11PublicClass staticcarfactory {1213PrivateStatic map<string, car> cars =New hashmap<string, car> ();1415 static {16 cars.put ( "Audi ", new Car (" Audi ", 300000));  Cars.put ( "Ford", new Car ( "Ford", 300000)); 18} 19 /**20 * Static Factory method * @ Param Name22 *  @return23 */24 public static Car getcar (String name) {25 26  return Cars.get (name); 27} 28}              

Instance Factory class:

 1Package com.yl.factory;23Import Java.util.HashMap;4Import Java.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 Bean instance 7 *@author Yul 8 * 9 * *10PublicClass instancecarfactory {1112Private map<string, car> cars =New hashmap<string, car> (); public  instancecarfactory () { hashmap<string = new, car> ();  Cars.put ("Audi", new Car ("Audi", 300000));  Cars.put ("Ford", New Car ("Ford", 400000));  Public  Car getcar (String brand) { cars.get (brand);                   +} 

Configuration file for the factory method:

1<?XML version= "1.0" encoding= "UTF-8"?> 2<Beansxmlns="Http://www.springframework.org/schema/beans"3Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"4xsi: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 attribute: 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 parameter 11-- >12<BeanId="Car1"13class="Com.yl.factory.StaticCarFactory"14Factory-method="Getcar" >15<Constructor-argValue="Audi" ></Constructor-arg>16</bean>17 18<!--Configure an instance of the factory-->19<BeanId="Carfactory"class="Com.yl.factory.InstanceCarFactory" ></bean>20 21<!--Factory-bean: Bean23 Factory-method to the instance factory method: Name of the instance factory method Constructor-arg: If the instance factory method requires an incoming parameter, Use Constructor-arg to configure the parameter-->26<!-- Configure Bean-->27 <bean id="car2" factory-bean="Carfactory " through the instance factory method factory-method="Getcar" >28 <constructor-arg value="Ford" ></  constructor-arg>29 </bean>30 </beans>      

Configuring Beans with Factorybean

The implementation class that inherits Factorybean:

 1Package Com.yl.factorybean;23Import Org.springframework.beans.factory.FactoryBean;4//Custom Factorybean requires implementation of Factorybean interface5PublicClass CarfactorybeanImplementsfactorybean<car> {67Private String brand;89Publicvoid Setbrand (String brand) {10This.brand = brand;11}1213/**14 * Returns the Bean Object 15 */16@Override17Public Car GetObject ()Throws Exception {18//TODO auto-generated Method Stub19ReturnNew Car ("BMW",600000);20}21st/**22 * return bean type */24  @Override 25 public class<?> getobjecttype () {26 // TODO auto-generated method Stub27 return car.class; 28} 29 30  @Override 31 public boolean Issingleton () {32   TODO auto-generated method Stub33 return true; 34} 35 36}           

Configuration file:

1<?XML version= "1.0" encoding= "UTF-8"?> 2<Beansxmlns="Http://www.springframework.org/schema/beans"3Xmlns:xsi="Http://www.w3.org/2001/XMLSchema-instance"4xsi:schemalocation="Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" > 5<!-- 6 using Factorybean to configure an instance of a Bean 7 class: A full class name pointing to Factorybean 8 property: Configuring Factorybean Properties 9 10 But the actual instance returned is the instance returned by Factorybean's GetObject () method-->12 <bean id="car" class=" Com.yl.factorybean.CarFactoryBean ">13 <property name=" brand " value=" BMW "> </property>14 </bean>15 </beans>     

Transferred from: HTTP://WWW.TUICOOL.COM/ARTICLES/MM2Q6RJ

Spring Configuration Bean method

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.