Spring FactoryBean 使用

Spring FactoryBean 使用

Entity:

 

package cn.rayoo.spring.factorybean;

public class Car {

	private String brand;

	private int speed;

	private Double price;
	
	private long id = System.currentTimeMillis();
	

	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	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;
	}

	public int getSpeed() {
		return speed;
	}

	public void setSpeed(int speed) {
		this.speed = speed;
	}

	public Car() {
	}

	public Car(String brand, int speed, Double price) {
		this.brand = brand;
		this.speed = speed;
		this.price = price;
	}

}

 FactoryBean:

 

 

package cn.rayoo.spring.factorybean;

import org.springframework.beans.factory.FactoryBean;

public class CarFactoryBean implements FactoryBean<Car> {

	public Car getObject() throws Exception { // 得到返回实体对象
		String[] infos = carInfo.split(",");

		Car car = new Car();

		car.setBrand(infos[0]);
		car.setSpeed(Integer.parseInt(infos[1]));
		car.setPrice(Double.parseDouble(infos[2]));

		return car;
	}

	public Class<Car> getObjectType() { // 得到返回实体类型
		return null;
	}

	public boolean isSingleton() { // 得到返回实体是否是单例模式
		return true;
	}

	private String carInfo;

	public String getCarInfo() {
		return carInfo;
	}

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

}

 ApplicationContext.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-4.1.xsd"
	default-lazy-init="true">

	<!-- scope="singleton/prototype" -->
	<bean id="car1" class="cn.rayoo.spring.factorybean.CarFactoryBean"
		p:carInfo="红旗CB01,100,56.00" />

</beans>

 Junit Test:

 

package cn.rayoo.spring.test;

import java.util.Random;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.junit.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.rayoo.spring.factorybean.Car;
import cn.rayoo.spring.factorybean.CarFactoryBean;

public class FactoryBeanTest {

	@Test
	public void testCarFactoryBean() {
		try {
			final ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
			final Random random = new Random();
			ExecutorService executorService = Executors.newFixedThreadPool(4);
			for (int i = 0; i < 100000; i++) {
				executorService.execute(new Runnable() {
					public void run() {
						try {
							Car car = (Car) ctx.getBean("car1");
							System.out.println(car.getId() + " " + " " + car.getBrand());
							// 以下方式可以获取CarFactoryBean,但不可以用于获取car1对象
							// CarFactoryBean factoryBean = (CarFactoryBean) ctx.getBean("&car1");
							// System.out.println(factoryBean.getObject().getId() + " " + " " + factoryBean.getObject().getBrand());
							Thread.sleep(46 * random.nextInt(5));
						} catch (Exception e) {
							e.printStackTrace();
						}
					}
				});
			}
			Thread.sleep(1000 * 4);
			ctx.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}

 

其中: 配置文件的 scope="singleton/prototype" 配置 会覆盖掉 cn.rayoo.spring.factorybean.CarFactoryBean.isSingleton() 方法的返回值, 所以是否单例以配置文件为准, 配置文件如果没有声明, 则以FactoryBean.isSingleton()方法的返回值为准!

 

 

 

 

你可能感兴趣的:(FactoryBean)