11.Spring通过工厂方法配置Bean

通过工厂方法配置Bean暴扣静态工厂方法和实例工厂方法。

1.静态工厂方法

     调用静态工厂方法创建Bean是将对象创建的过程封装到静态方法中,当客户端需要对象时,只需要简单的调用静态方法,而不去关心创建对象的细节。

  要声明通过静态方法创建的Bean,需要在Bean的class属性里指定拥有该工厂的方法的类,同时在factory-method属性里指定工厂方法的名称,最后,使用<constructor-arg>元素为该方法传递方法参数。

public class Car {
    private  String brand;
    private  double price;

    public Car() {
        System.out.println("com.cn.Car's Constructor");
    }

    public Car(String brand, double price) {
        this.brand = brand;
        this.price = price;
    }

    public void setBrand(String brand) {
        System.out.println("setBrand");
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}
/**
 * 静态工厂方法:直接调用某一个类的静态方法,就可以返回Bean的实例
 */
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", 400000));
    }

    //静态工厂方法
    public static Car getCar(String name) {
        return cars.get(name);
    }
}
<?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实例-->
    <!--
    class属性:指向静态工厂方法的全类名
    factory-method:指向静态工厂方法的名字
    constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数
    -->
    <bean id="car1" class="factory.StaticCarFactory" factory-method="getCar">
        <constructor-arg value="audi"></constructor-arg>
    </bean>
</beans>
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("11.xml");
        Car car1 = (Car) ctx.getBean("car1");
        System.out.println(car1);
    }
}

2.实例工厂方法

  将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时,只需要简单的调用该实例方法而不需要关心对象的创建细节。

  要声明通过实例工厂方法创建的Bean。在Bean的factory-bean属性里指定拥有该工厂方法的Bean,同时在factory-method属性里指定工厂方法的名称,最后,使用<constructor-arg>元素为该方法传递方法参数。

public class Car {
    private  String brand;
    private  double price;

    public Car() {
        System.out.println("com.cn.Car's Constructor");
    }

    public Car(String brand, double price) {
        this.brand = brand;
        this.price = price;
    }

    public void setBrand(String brand) {
        System.out.println("setBrand");
        this.brand = brand;
    }

    public String getBrand() {
        return brand;
    }

    public void setPrice(double price) {
        this.price = price;
    }

    public double getPrice() {
        return price;
    }

    @Override
    public String toString() {
        return "Car{" +
                "brand='" + brand + '\'' +
                ", price=" + price +
                '}';
    }
}
/**
 * 实例工厂方法:实例工厂的方法,即先需要创建工厂本身,在调用工厂的实例方法来返回bean的实例
 */
public class InstanseCarFactory {
    private Map<String, Car> cars = null;

    public InstanseCarFactory() {
        cars = new HashMap<String, Car>();
        cars.put("audi", new Car("audi", 300000));
        cars.put("ford", new Car("ford", 400000));
    }

    public Car getCar(String brand) {
        return cars.get(brand);
    }
}
<?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="carFactory" class="factory.InstanseCarFactory"></bean>
    <!--通过实例工厂方法来配置bean-->
    <!--
        factory-bean:指向实例工厂方法的bean
        factory-method:指向实例工厂方法的名字
        constructor-arg:如果工厂方法需要传入参数,则使用constructor-arg来配置参数
    -->
    <bean id="car" factory-bean="carFactory" factory-method="getCar">
        <constructor-arg value="ford"></constructor-arg>
    </bean>
</beans>
public class Main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("11-2.xml");
        Car car = (Car) ctx.getBean("car");
        System.out.println(car);
    }
}

posted on 2015-06-19 10:54  jec  阅读(142)  评论(0编辑  收藏  举报

导航