码迷,mamicode.com
首页 > 编程语言 > 详细

Spring使用教程(二)配置bean:静态工厂方法和实例工厂方法

时间:2015-05-29 23:00:57      阅读:199      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
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(String brand, double price) {
        super();
        this.brand = brand;
        this.price = price;
    }
    
}
View Code
技术分享
/**
 * 静态工厂方法:直接调用某一个类的静态方法就可以返回bean的实例
 * */
public class StaticCarFactory {

    private static Map<String,Car> cars=new HashMap<String, Car>();
    static{
        cars.put("BMW",new Car("BMW",300000));
        cars.put("benchi",new Car("benchi",690000));
    }
    //静态工厂方法
    public static Car getCar(String name){
        return cars.get(name);
    }    
}
View Code
技术分享
    <?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:util="http://www.springframework.org/schema/util"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
            <!-- 通过静态工厂方法配置Bean,注意不是配置静态工厂方法实例,而是bean实例 -->
            
            <!-- 
                calss:指向静态工厂方法的全类名
                factory-method:指向静态工厂方法的名字
                constructor-arg:如果工厂静态方法需要参数,则使用constructor-arg来配置
             -->
            
            <bean id="carfactory" class="com.test.spring.factorybean.StaticCarFactory" factory-method="getCar">
                <constructor-arg value="BMW"></constructor-arg>        <!-- 构造器注入 -->
            </bean>
        </beans>
View Code
技术分享
    public class Main {
        public static void main(String[] args) {
            ApplicationContext con=new ClassPathXmlApplicationContext("carFactory.xml");
            Car car=(Car)con.getBean("carfactory");
            System.out.println(car);
        }
    }
View Code

 实例工厂方法

技术分享
/**
 * 
 * 实例工厂方法:实例工厂的方法,先创建工厂本身,在调用工厂本身的实例方法返回bean实例
 * 
 * */

public class InstanceFactory {

    private Map<String,Car> cars=null;
    
    public InstanceFactory(){
        cars=new HashMap<String, Car>();
        cars.put("BMW",new Car("BMW",300000));
        cars.put("benchi",new Car("benchi",333300000));        
    }
    
    public Car getCar(String brand){
        return cars.get(brand);
    }
}
View Code
<?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:util="http://www.springframework.org/schema/util"
        xmlns:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-4.0.xsd">
            <!-- 通过静态工厂方法配置Bean,注意不是配置静态工厂方法实例,而是bean实例 -->
            
            <!-- 
                calss:指向静态工厂方法的全类名
                factory-method:指向静态工厂方法的名字
                constructor-arg:如果工厂静态方法需要参数,则使用constructor-arg来配置
             -->
            
            <bean id="carfactory" class="com.test.spring.factorybean.StaticCarFactory" factory-method="getCar">
                <constructor-arg value="BMW"></constructor-arg>        <!-- 构造器注入 -->
            </bean>
            
            <!-- 配置工厂的实例 -->
            <bean id="instanceFactory" class="com.test.spring.factorybean.InstanceFactory"></bean>
            <!-- 通过实例工厂方法类配置bean -->
            <bean id="car" factory-bean="instanceFactory" factory-method="getCar">
                <constructor-arg value="benchi"></constructor-arg>
            </bean>
        </beans>

 

Spring使用教程(二)配置bean:静态工厂方法和实例工厂方法

标签:

原文地址:http://www.cnblogs.com/img-zoom/p/4539499.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!