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