Spring use Tutorial (ii) Configuring beans: Static factory methods and instance factory methods

Source: Internet
Author: User

 Public classCar {PrivateString brand; Private DoublePrice ;  PublicString Getbrand () {returnbrand; }     Public voidSetbrand (String brand) { This. Brand =brand; }     Public DoubleGetPrice () {returnPrice ; }     Public voidSetprice (DoublePrice ) {         This. Price =Price ; } @Override PublicString toString () {return"Car [brand=" + Brand + ", price=" + Price + "]"; }     PublicCar (String brand,DoublePrice ) {        Super();  This. Brand =brand;  This. Price =Price ; }    }
View Code
/*** Static Factory method: A static method that calls a class directly can return an instance of a bean*/ Public classStaticcarfactory {Private StaticMap<string,car> cars=NewHashmap<string, car>(); Static{cars.put ("BMW",NewCar ("BMW", 300000)); Cars.put ("Benchi",NewCar ("Benchi", 690000)); }    //Static Factory Method     Public StaticCar Getcar (String name) {returncars.get (name); }    }
View Code
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:util= "Http://www.springframework.org/schema/util"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http ://Www.springframework.org/schema/utilhttp://www.springframework.org/schema/util/spring-util-4.0.xsd"><!--Configure the Bean with a static factory method, noting that the static factory method instance is not configured, but the bean instance--<!--CALSS: A full-class name that points to the static factory method factory-Method : Name pointing to static factory methods constructor-arg: If the factory static method requires parameters, use constructor-Arg to configure--<bean id= "Carfactory"class= "Com.test.spring.factorybean.StaticCarFactory" factory-method= "Getcar" > <constructor-arg value= "BMW" ></constructor-arg> <!--Builder Injection--</bean> </beans>
View Code
     Public class Main {        publicstaticvoid  main (string[] args) {            ApplicationContext Con=new classpathxmlapplicationcontext ("Carfactory.xml");            Car Car= (car) con.getbean ("Carfactory");            SYSTEM.OUT.PRINTLN (car);        }    }
View Code

Instance Factory method

/*** Example Factory Method: Instance Factory method, first creates the factory itself, returns the bean instance by invoking the instance method of the factory itself * **/ Public classInstancefactory {PrivateMap<string,car> cars=NULL;  Publicinstancefactory () {Cars=NewHashmap<string, car>(); Cars.put ("BMW",NewCar ("BMW", 300000)); Cars.put ("Benchi",NewCar ("Benchi", 333300000)); }         PublicCar Getcar (String brand) {returnCars.get (brand); }}
View Code
<?XML version= "1.0" encoding= "UTF-8"?>    <Beansxmlns= "Http://www.springframework.org/schema/beans"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"Xmlns:util= "Http://www.springframework.org/schema/util"xmlns:p= "http://www.springframework.org/schema/p"xsi:schemalocation= "Http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd Http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd ">            <!--Configure the Bean with a static factory method, note that the bean instance is not configured with a static factory method instance -                        <!--CALSS: Full class name pointing to the static factory method Factory-method: Name of the static factory method Constructor-arg: If the factory static method requires parameter, use Constructor-arg to configure the -                        <BeanID= "Carfactory"class= "Com.test.spring.factorybean.StaticCarFactory"Factory-method= "Getcar">                <Constructor-argvalue= "BMW"></Constructor-arg>        <!--Constructor Injection -            </Bean>                        <!--Configure an instance of the factory -            <BeanID= "Instancefactory"class= "Com.test.spring.factorybean.InstanceFactory"></Bean>            <!--configuring beans through the instance factory method class -            <BeanID= "Car"Factory-bean= "Instancefactory"Factory-method= "Getcar">                <Constructor-argvalue= "Benchi"></Constructor-arg>            </Bean>        </Beans>

Spring use Tutorial (ii) Configuring beans: Static factory methods and instance factory methods

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.