通过调用静态工厂方法创建Bean
1)调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中。当客户端需要对象时,只需要简单地调用静态方法,而不需要关心创建对象的具体细节。
2)要声明通过静态方法创建的Bean,需要在Bean的class属性中指定拥有该工厂的方法的类,同时需要在bean的factory-method属性里指定工厂方法的名称。最后,使用<constructor-arg>元素为该方法传递方法参数。
示例:
第一步:创建一个java project,导入包:
第二步:创建com.dx.spring.beans.factory.Car.java,com.dx.spring.beans.factory.StaticCarFactory.java,bean-factory.xml:
Car.java
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.dx.spring.beans.factory; public class Car { private String brand; private double price; public Car() { } public Car(String brand, double price) { super(); this.brand = brand; this.price = price; } public String getBrand() { return brand; } public void setBrand(String brand) { this.brand = brand; } public double getPrice() { return price; } public void setPrice(double price) { this.price = price; } @Override public String toString() { return "Car [brand=" + brand + ", price=" + price + "]"; } }
StaticCarFactory.java
package com.dx.spring.beans.factory; import java.util.HashMap; import java.util.Map; public class StaticCarFactory { private static Map<String, Car> cars = null; static { cars = new HashMap<>(); cars.put("audi", new Car("AUDI", 350000)); cars.put("ford", new Car("FORD", 200000)); } public static Car getCar(String key) { return cars.get(key); } }
bean-factory.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id="car1" class="com.dx.spring.beans.factory.StaticCarFactory" factory-method="getCar"> <constructor-arg name="key" value="audi"></constructor-arg> </bean> </beans>
第三步:添加Client.java测试类,并执行测试:
package com.dx.spring.beans.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factory.xml"); Car car1 = (Car) ctx.getBean("car1"); System.out.println(car1); } }
发现执行结果成功返回car1:
Car [brand=AUDI, price=350000.0]
通过调用实例工厂方法创建Bean
1)实例工行方法:将对象的创建过程封装到另外一个对象实例的方法里,当客户端需要请求对象时,只需要简单的调用该实例方法而不需要关心对象的创建细节。
2)需要声明通过实例工厂方法创建的Bean:
---- 在bean的factory-bean属性里指定拥有该工厂方法的Bean;
---- 在factory-method属性里指定该工厂方法的名称;
---- 使用<constructor-arg>元素为工厂方法传递方法参数。
示例:(备注:基于上边的示例修改)
第一步:创建com.dx.spring.beans.factory.InstanceCarFactory.java
package com.dx.spring.beans.factory; import java.util.HashMap; import java.util.Map; public class InstanceCarFactory { private Map<String, Car> cars = null; public InstanceCarFactory() { cars = new HashMap<>(); cars.put("audi", new Car("audi", 350000)); cars.put("ford", new Car("ford", 200000)); } public Car getCar(String key) { return cars.get(key); } }
第二步:在bean-factory.xml(spring bean配置文件)中添加如下配置:
<bean id="instanceCarFactory" class="com.dx.spring.beans.factory.InstanceCarFactory"></bean> <bean id="car2" factory-bean="instanceCarFactory" factory-method="getCar"> <constructor-arg name="key" value="ford"></constructor-arg> </bean>
第三步:修改Client.java,添加car2测试:
package com.dx.spring.beans.factory; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Client { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factory.xml"); Car car1 = (Car) ctx.getBean("car1"); System.out.println(car1); Car car2 = (Car) ctx.getBean("car2"); System.out.println(car2); } }
测试结果为:
Car [brand=AUDI, price=350000.0]
Car [brand=ford, price=200000.0]