十一、工厂方法配置Bean
通过全类名方法配置Bean底层采用的是反射,除此之外还可以通过工厂方法(静态工厂方法&实例工厂方法)、FactoryBean来配置Bean。
静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单地调用静态方法而不需要关系创建对象的细节。
要声明通过静态方法调用Bean,需要在Bean的class属性里指定该工厂的方法的类,同时在factory-method属性中指定工厂方法的名称,最后使用<constructor-arg>
元素为该方法传递方法参数。
11.1静态工厂方法
Car类:
package com.stuspring.factory;
/**
* Created by bee on 17/4/28.
*/
public class Car {
private String brand;
private double price;
public void setBrand(String brand) {
this.brand = brand;
}
public void setPrice(double price) {
this.price = price;
}
public String getBrand() {
return brand;
}
public double getPrice() {
return price;
}
public Car() {
}
public Car(String brand, double price) {
this.brand = brand;
this.price = price;
}
@Override
public String toString() {
return "Car{" +
"brand='" + brand + '\'' +
", price=" + price +
'}';
}
}
静态工厂方法:
package com.stuspring.factory;
import java.util.HashMap;
import java.util.Map;
/**
* Created by bee on 17/4/28.
*/
public class StaticCarFactory {
private static Map<String,Car> cars=new HashMap<String, Car>();
static {
cars.put("audi",new Car("Audi",300000));
cars.put("ford",new Car("Ford",200000));
}
//静态工厂方法
public static Car getCar(String name){
return cars.get(name);
}
}
配置Bean:
<?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实例。注意,不是静态工厂方法实例-->
<bean id="car1" class="com.stuspring.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="audi"/>
</bean>
</beans>
测试:
package com.stuspring.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by bee on 17/4/28.
*/
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1);
}
}
运行结果:
Car{brand='Audi', price=300000.0}
11.2 实例工厂方法
package com.stuspring.factory;
import java.util.HashMap;
import java.util.Map;
/**
* Created by bee on 17/4/28.
*/
public class InstanceCarFactory {
private Map<String,Car> cars=null;
public InstanceCarFactory(){
cars=new HashMap<String, Car>();
cars.put("audi",new Car("Audi",300000));
cars.put("ford",new Car("Ford",400000));
}
public Car getCar(String carname){
return cars.get(carname);
}
}
配置Bean:
<!--配置工厂实例-->
<bean id="instanceFactory" class="com.stuspring.factory.InstanceCarFactory"/>
<bean id="car2" factory-bean="instanceFactory" factory-method="getCar">
<constructor-arg value="ford"/>
</bean>
测试:
Car car2= (Car) ctx.getBean("car2");
System.out.println(car2);
打印结果:
Car{brand='Ford', price=400000.0}
十二、FactoryBean配置Bean
Spring中有2种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean。工厂Bean跟普通Bean不同,其返回的对象不是指定类的一个实例,而是工厂Bean的getObjective方法所返回的对象。
创建一个CarFactoryBean类,继承FactoryBean接口:
package com.stuspring.factorybean;
import org.springframework.beans.factory.FactoryBean;
/**
* Created by bee on 17/4/28.
*/
public class CarFactoryBean implements FactoryBean<Car>{
private String brand;
public void setBrand(String brand) {
this.brand = brand;
}
public Car getObject() throws Exception {
return new Car(brand,8000000);
}
public Class<?> getObjectType() {
return Car.class;
}
public boolean isSingleton() {
return false;
}
}
配置Bean:
<?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.stuspring.factorybean.CarFactoryBean">
<property name="brand" value="Ford"/>
</bean>
<bean id="car2" class="com.stuspring.factorybean.CarFactoryBean">
<property name="brand" value="BMW"/>
</bean>
</beans>
测试:
package com.stuspring.factorybean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* Created by bee on 17/4/28.
*/
public class Main {
public static void main(String[] args) {
ApplicationContext ctx=new ClassPathXmlApplicationContext("beans-beanfactory.xml");
Car car1= (Car) ctx.getBean("car1");
System.out.println(car1);
Car car1Copy= (Car) ctx.getBean("car1");
System.out.println(car1Copy);
System.out.println(car1==car1Copy);
Car car2= (Car) ctx.getBean("car2");
System.out.println(car2);
}
}
打印结果:
Car{brand='Ford', price=8000000.0}
Car{brand='Ford', price=8000000.0}
false
Car{brand='BMW', price=8000000.0}