Spring全家桶(五)Bean的多种配置方法

十一、工厂方法配置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}

13.通过注解配置Bean

13.1基于注解配置Bean

13.2基于注解来装配Bean的属性

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

esc_ai

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值