FactoryBean 接口

Spring 中有两种类型的Bean,一种是普通Bean,另一种是工厂Bean,即FactoryBean,这两种Bean都被容器管理,但工厂Bean跟普通 Bean不同,其返回的对象不是指定类的一个实例,其返回的是该FactoryBean的getObject方法所返回的对象。

接口FactoryBean是由Spring提供的工厂Bean的标准接口,提供了如下3个方法

(1)Object getObject();

(2)Class getObject();

(3)boolean isSingleton()

实例:

public class Car {
	private String maxSpeed;
	private String brand;
	private String price;
	public String getMaxSpeed() {
		return maxSpeed;
	}
	public void setMaxSpeed(String info) {
		this.maxSpeed = info;
	}
	public String getBrand() {
		return brand;
	}
	public void setBrand(String brand) {
		this.brand = brand;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	
}

 

public class CarFactoryBean implements FactoryBean<Car>{
	private String carInfo;
	@Override
	public Car getObject() throws Exception {
		Car car=new Car();
		String[] info=carInfo.split(",");
		car.setMaxSpeed(info[0]);
		car.setBrand(info[1]);
		car.setPrice(info[2]);
		return car;
	}

	@Override
	public Class<Car> getObjectType() {
		
		return Car.class;
	}

	@Override
	public boolean isSingleton() {
		
		return false;
	}

	public String getCarInfo() {
		return carInfo;
	}

	public void setCarInfo(String carInfo) {
		this.carInfo = carInfo;
	}
	
}

 

<bean id="car" class="application.CarFactoryBean">
		<property name="carInfo">
			<value>100米每个小时,奔驰,1000元</value>
		</property>
	</bean>

 

public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("config/applicationContext.xml");
		Car car=(Car)context.getBean("car");
		System.out.println("品牌:"+car.getBrand());
		System.out.println("最大速度:"+car.getMaxSpeed());
		System.out.println("价格:"+car.getPrice());
	}

结果:品牌:奔驰
最大速度:100米每个小时
价格:1000元

 该实例中容器通过getBean返回来的不是CarFactoryBean本身,而是该类getObject方法中创建的实例Car,要想获得CarFactoryBean,可以通过getBean("&car")来获取

public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("config/applicationContext.xml");
		CarFactoryBean car=(CarFactoryBean)context.getBean("&car");
		System.out.println("汽车基本信息:"+car.getCarInfo());
		
	}

结果:汽车基本信息:100米每个小时,奔驰,1000元

 factorybean的另外一种实现方式:实例工厂

扫描二维码关注公众号,回复: 846821 查看本文章
public class CarFactoryBean{
	
	
	public  Car getCar(String carInfo){
		Car car=new Car();
		String[] info=carInfo.split(",");
		car.setMaxSpeed(info[0]);
		car.setBrand(info[1]);
		car.setPrice(info[2]);
		return car;
	
	}
}

 

<bean id="carFactory" class="application.CarFactoryBean">
		
	</bean>
	<bean id="car" factory-bean="carFactory" factory-method="getCar">
		<constructor-arg><value>2930,239,3090</value></constructor-arg>
	</bean>

 其中factory-bean是指实例工厂,factory-method是指实例工厂的方法,constructor-arg是为实例工厂的方法传入参数

public static void main(String[] args) {
		ApplicationContext context=new ClassPathXmlApplicationContext("config/applicationContext.xml");
		Car car=(Car)context.getBean("car");
		System.out.println("价格:"+car.getPrice());
		System.out.println("品牌:"+car.getBrand());
		System.out.println("速度:"+car.getMaxSpeed());
	}

结果:价格:3090
品牌:239
速度:2930

 

猜你喜欢

转载自201407105131.iteye.com/blog/2204346