Spring4.0学习笔记(6) —— 通过工厂方法配置Bean

1、静态工厂方法:

bean

package com.spring.factory;

public class Car {
    
    public Car(String brand) {
        this.brand = brand;
    }

    @Override
    public String toString() {
        return "Car [brand=" + brand + "]";
    }

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

2、静态工厂类

package com.spring.factory;

import java.util.HashMap;
import java.util.Map;

/*
 * 静态工厂:直接调用某一个类的静态方法返回bean的实例
 */
public class staticCarFactory {

    private static Map<String, Car> cars = new HashMap<String, Car>();
    
    static{
        cars.put("audi", new Car("audi"));
        cars.put("nission", new Car("nission"));
    }
    
    //在不创建类的对象的条件下,通过静态方法返回对应的实例
    public static Car getCar(String name){
        return cars.get(name);
    }
}

3、main方法

package com.spring.factory;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class main {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factory.xml");
        Car car = (Car)ctx.getBean("factory");
        System.out.println(car);
    }
}

4、配置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"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">


    <!-- class 属性 指向静态方法的全类名
         factory-method 指向静态方法
         constructor-arg 如果工厂方法需要传入参数,则使用constructor-arg 来配置参数
     -->
    <bean id="factory" class="com.spring.factory.staticCarFactory" 
        factory-method="getCar">
            <constructor-arg value="nission"></constructor-arg>
    </bean>
</beans>

  工厂方法:

package com.spring.factory;

import java.util.HashMap;
import java.util.Map;

public class instanceCarFactory {
    
    private Map<String, Car> cars = new HashMap<String, Car>();
    
    public instanceCarFactory() {
        cars = new HashMap<String, Car>();
        cars.put("audi", new Car("audi"));
        cars.put("ford", new Car("ford"));
    }
    
    public Car getCar(String name){
        return cars.get(name);
    }
}

xml

    <!-- 配置工厂的实例 -->
    <bean id="instanceFactory" class="com.spring.factory.instanceCarFactory"></bean>
    <!--  -->    
    <bean id="car2" factory-bean="instanceFactory" factory-method="getCar">
        <constructor-arg value="ford"></constructor-arg>
    </bean>
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("bean-factory.xml");
        Car car = (Car)ctx.getBean("car2");
        System.out.println(car);
    }

 

posted @ 2016-03-20 21:55  cklovefan  阅读(236)  评论(0编辑  收藏  举报