Spring学习第十一天:通过工厂方式配置Bean

来源:互联网 发布:js 修改样式 编辑:程序博客网 时间:2024/03/29 06:24

工厂方式配置bean可分为两种:静态工厂方法和实例工厂方法

静态工厂方法
调用静态工厂方法创建 Bean是将对象创建的过程封装到静态方法中. 当客户端需要对象时, 只需要简单地调用静态方法, 而不同关心创建对象的细节.
要声明通过静态方法创建的 Bean, 需要在 Bean 的 class 属性里指定拥有该工厂的方法的类, 同时在 factory-method 属性里指定工厂方法的名称. 最后, 使用 < constrctor-arg> 元素为该方法传递方法参数.

新建一个包,com.atguigu.spring.beans.factory

新建类 Car:

package com.atguigu.spring.beans.factory;public class Car {    private String brand;    private double 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 + "]";    }    public Car() {        // TODO Auto-generated constructor stub        System.out.println("Car's constructor...");    }    public Car(String brand, double price) {        super();        this.brand = brand;        this.price = price;    }}

新建一个StaticCarFactory类

package com.atguigu.spring.beans.factory;import java.util.HashMap;import java.util.Map;/*** * 静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例 * @author asus * */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);    }}

上面的getCar就是一个静态工厂方法,我们在不需要创建StaticCarFactory实例的情况下就可以直接获得Car实例。

现在我们要配置Car去直接获得Car的实例,注意,配置Car,而不是配置StaticCarFactory

新建 beans-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,注意不是配置静态工厂方法实例,而是配置bean实例 -->    <!--         class 属性: 指向静态工厂方法的全类名        factory-method: 指向静态方法的名字        constructor-arg: 如果工厂方法需要传入参数,则使用constructor-arg来配置参数     -->    <bean id="car1" class="com.atguigu.spring.beans.factory.StaticCarFactory" factory-method="getCar">        <constructor-arg value="audi"></constructor-arg>    </bean></beans>

新建Main 类

package com.atguigu.spring.beans.factory;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {    public static void main(String[] args) {        // TODO Auto-generated method stub        ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");        Car car1 = (Car) ctx.getBean("car1");        System.out.println(car1);    }}

输出结果如下:

Car [brand=audi, price=300000.0]

实例工厂方法
实例工厂方法: 将对象的创建过程封装到另外一个对象实例的方法里. 当客户端需要请求对象时, 只需要简单的调用该实例方法而不需要关心对象的创建细节.
要声明通过实例工厂方法创建的 Bean
在 bean 的 factory-bean 属性里指定拥有该工厂方法的 Bean
在 factory-method 属性里指定该工厂方法的名称
使用 construtor-arg 元素为工厂方法传递方法参数

新建InstanceCarFactory类

package com.atguigu.spring.beans.factory;import java.util.HashMap;import java.util.Map;/*** * 实例工厂方法: 实例工厂的方法,即先需要创建工厂本身,再调用工厂的实例方法来返回bean的实例 * @author asus * */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 brand){        return cars.get(brand);    }}

xml添加如下配置:

<!-- 配置工厂的实例 --><bean id="carFactory" class="com.atguigu.spring.beans.factory.InstanceCarFactory"></bean><!-- 通过实例工厂方法来配置bean --><!--     factory-bena 属性: 指向静态工厂方法的bean    factory-method: 指向静态方法的名字    constructor-arg: 如果工厂方法需要传入参数,则使用constructor-arg来配置参数 --><bean id="car2" factory-bean="carFactory" factory-method="getCar">    <constructor-arg value="ford"></constructor-arg></bean>

Main类:

ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");Car car2 = (Car) ctx.getBean("car2");System.out.println(car2);

输出结果如下:

Car [brand=ford, price=400000.0]
0 0
原创粉丝点击