通过工厂方法配置bean
1.通过调用静态工厂方法创建bean
调用静态工厂方法创建bean是将对象创建的过程封装到静态方法中。当客户端需要对象时,只需要简单地调用静态方法,而不用关心创建对象的细节。
要声明通过静态方法创建的bean,需要在bean的class属性里指定拥有该工厂的方法的类,通知在factory-method属性里指定工厂方法的名称,最后,使用<constructor-arg>元素为该方法传递方法参数。
2.通过调用实例工厂方法创建bean
实例工厂方法:将对象的创建过程封装到另外一个对象实例的方法里。当客户端需要请求对象时,只需要简单的调用该实例方法而不需关心对象的创建细节。
要声明通过实例工厂方法创建的bean:-在bean的 factory-bean属性里指定拥有该工厂方法的bean
-在 factory-method属性里指定该工厂方法的名称
-使用constructor-arg元素为工厂方法传递方法参数
bean类:
package com.atguigu.spring.factory;
/**
* bean
* @author xiejianwei
*
*/
public class Car {
private String brand;
private float price;
public Car() {
}
public Car(String brand, float price) {
super();
this.brand = brand;
this.price = price;
}
/**
* @return the brand
*/
public String getBrand() {
return brand;
}
/**
* @param brand the brand to set
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* @return the price
*/
public float getPrice() {
return price;
}
/**
* @param price the price to set
*/
public void setPrice(float price) {
this.price = price;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "Car [brand=" + brand + ", price=" + price + "]";
}
}
静态工厂类:
package com.atguigu.spring.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 静态工厂方法:直接调用某一个类的静态方法就可以返回 Bean 的实例
* @author xiejianwei
*
*/
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));
}
/**
* 静态工厂方法
* @param name
* @return
*/
public static Car getCar(String name) {
return cars.get(name);
}
}
实例工厂类:
package com.atguigu.spring.factory;
import java.util.HashMap;
import java.util.Map;
/**
* 实例工厂方法:实例工厂的方法,即先需要创建工厂本身,再调用工厂的实例方法来返回bean的实例
* @author xiejianwei
*
*/
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 name) {
return cars.get(name);
}
}
工厂方法的配置文件 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.factory.StaticCarFactory"
factory-method="getCar">
<constructor-arg value="audi"></constructor-arg>
</bean>
<!-- 配置工厂的实例 -->
<bean id="carFactory" class="com.atguigu.spring.factory.InstanceCarFactory"></bean>
<!-- 通过实例工厂方法来配置bean -->
<!--
factory-bean:指向实例工厂方法的bean
factory-method:指向实例工厂方法的名字
constructor-arg:如果实例工厂方法需要传入参数,则使用constructor-arg来配置参数
-->
<bean id="car2" factory-bean="carFactory" factory-method="getCar">
<constructor-arg value="ford"></constructor-arg>
</bean>
</beans>
测试类:
package com.atguigu.spring.factory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* 测试类
* @author xiejianwei
*
*/
public class Main {
public static void main(String[] args) {
//创建 IOC 容器
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-factory.xml");
Car car1 = (Car) ctx.getBean("car1");
System.out.println(car1);
Car car2 = (Car) ctx.getBean("car2");
System.out.println(car2);
}
}
运行结果:
Car [brand=audi, price=300000.0]
Car [brand=ford, price=400000.0]
通过Factorybean配置bean:
package com.atguigu.spring.factorybean;
import org.springframework.beans.factory.FactoryBean;
/**
* 自定义的Factorybean需要实现FactoryBean接口
* @author xiejianwei
*
*/
public class CarFactoryBean implements FactoryBean<Car> {
private String brand;
/**
* @param brand the brand to set
*/
public void setBrand(String brand) {
this.brand = brand;
}
/**
* 返回bean的对象
*/
@Override
public Car getObject() throws Exception {
return new Car(brand, 500000);
}
/**
* 返回bean的类型
*/
@Override
public Class<?> getObjectType() {
return Car.class;
}
@Override
public boolean isSingleton() {
return true;
}
}
配置文件 beans-beanfactory.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">
<!--
通过Factorybean来配置bean的实例
class:指向Factorybean的全类名
property:配置Factorybean的属性
但实际返回的实例却是Factorybean的getObject()方法返回的实例
-->
<bean id="car" class="com.atguigu.spring.factorybean.CarFactoryBean">
<property name="brand" value="BMW"></property>
</bean>
</beans>
测试类:
package com.atguigu.spring.factorybean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-beanfactory.xml");
Car car = (Car) ctx.getBean("car");
System.out.println(car);
}
}
运行结果:
Car [brand=BMW, price=500000.0]