spring factory bean

Spring  FactoryBean 是创建 复杂的bean,一般的bean 直接用xml配置即可,如果一个bean的创建过程中涉及到很多其他的bean 和复杂的逻辑,用xml配置比较困难,这时可以考虑用FactoryBean

例子如下:

1:创建一个Car类(是为了简便)一般不能直接给出Car类,如果是这样直接注入就可以或者Car对象了,这里只是为了简便。

  1. <span style="font-size: 10px;">package com.myapp.core.factorybean;  
  2.   
  3. public class Car {  
  4.   private  String  make;  
  5.   private  int year;  
  6.     public String getMake() {  
  7.         return make;  
  8.     }  
  9.     public void setMake(String make) {  
  10.         this.make = make;  
  11.     }  
  12.     public int getYear() {  
  13.         return year;  
  14.     }  
  15.     public void setYear(int year) {  
  16.         this.year = year;  
  17.     }  
  18.         
  19. }</span><span style="font-size: 24px;">  
  20. </span>  
package com.myapp.core.factorybean;

public class Car {
  private  String  make;
  private  int year;
	public String getMake() {
		return make;
	}
	public void setMake(String make) {
		this.make = make;
	}
	public int getYear() {
		return year;
	}
	public void setYear(int year) {
		this.year = year;
	}
	  
}

2:一个FactoryBean的实现拥有创建car

  1. package com.myapp.core.factorybean;  
  2.   
  3. import org.springframework.beans.factory.FactoryBean;  
  4.   
  5. public class MyCarFactoryBean  implements FactoryBean<Car>{  
  6.       
  7.     private String  make;  
  8.       
  9.     private  int  year;  
  10.   
  11.     public void setMake(String make) {  
  12.         this.make = make;  
  13.     }  
  14.   
  15.     public void setYear(int year) {  
  16.         this.year = year;  
  17.     }  
  18.   
  19.     @Override  
  20.     public Car getObject() throws Exception {  
  21.         // TODO Auto-generated method stub  
  22.           
  23.         //Here  is a complex  car  object  created  
  24.          // wouldn't be a very useful FactoryBean  
  25.         // if we could simply instantiate the object!  
  26.         Car  car  = new  Car();  
  27.           
  28.         if(year != 0){  
  29.            car.setYear(this.year);  
  30.         }  
  31.           
  32.         if("make".equals(make)){  
  33.             car.setMake("we are making  bla bla  bla");  
  34.         }else{  
  35.             car.setMake(this.make);  
  36.         }  
  37.           
  38.         return  car;  
  39.     }  
  40.   
  41.     @Override  
  42.     public Class<?> getObjectType() {  
  43.         // TODO Auto-generated method stub  
  44.         return Car.class;  
  45.     }  
  46.   
  47.     @Override  
  48.     public boolean isSingleton() {  
  49.         // TODO Auto-generated method stub  
  50.         return false;  
  51.     }  
  52.   
  53. }  
package com.myapp.core.factorybean;

import org.springframework.beans.factory.FactoryBean;

public class MyCarFactoryBean  implements FactoryBean<Car>{
	
	private String  make;
	
	private  int  year;

	public void setMake(String make) {
		this.make = make;
	}

	public void setYear(int year) {
		this.year = year;
	}

	@Override
	public Car getObject() throws Exception {
		// TODO Auto-generated method stub
		
		//Here  is a complex  car  object  created
		 // wouldn't be a very useful FactoryBean
	    // if we could simply instantiate the object!
		Car  car  = new  Car();
		
		if(year != 0){
		   car.setYear(this.year);
		}
		
		if("make".equals(make)){
			car.setMake("we are making  bla bla  bla");
		}else{
			car.setMake(this.make);
		}
		
		return  car;
	}

	@Override
	public Class<?> getObjectType() {
		// TODO Auto-generated method stub
		return Car.class;
	}

	@Override
	public boolean isSingleton() {
		// TODO Auto-generated method stub
		return false;
	}

}

以上中创建car太简单了,如果太简单就没有必要用FactoryBean创建了,可以写的复杂些。

 

3:Person 引用一个car

 
  1. package com.myapp.core.factorybean;  
  2.   
  3. public class Person {  
  4.    
  5.     private  Car  car;  
  6.   
  7.     public Car getCar() {  
  8.         return car;  
  9.     }  
  10.   
  11.     public void setCar(Car car) {  
  12.         this.car = car;  
  13.     }  
  14.       
  15.     public  String  toString(){  
  16.           
  17.         return  car.getMake()+"::::"+car.getYear();  
  18.     }  
  19. }  
package com.myapp.core.factorybean;

public class Person {
 
	private  Car  car;

	public Car getCar() {
		return car;
	}

	public void setCar(Car car) {
		this.car = car;
	}
	
	public  String  toString(){
		
		return  car.getMake()+"::::"+car.getYear();
	}
}

4:配置引用xml格式:

 
  1. <bean id="car" class="com.myapp.core.factorybean.MyCarFactoryBean">  
  2.    <property name="make" value="makeing car"/>  
  3.    <property name="year"  value="123"/>  
  4. </bean>  
  5.   
  6. <bean id="person" class="com.myapp.core.factorybean.Person">  
  7.   
  8. <property name="car" ref="car"/>  
  9.   
  10. </bean>  
 <bean id="car" class="com.myapp.core.factorybean.MyCarFactoryBean">
    <property name="make" value="makeing car"/>
    <property name="year"  value="123"/>
 </bean>

 <bean id="person" class="com.myapp.core.factorybean.Person">
 
 <property name="car" ref="car"/>
 
 </bean>

5:编写测试类测试:

  1. package com.myapp.core.factorybean;  
  2.   
  3. import org.springframework.context.ApplicationContext;  
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  5.   
  6. public class MainTest {  
  7.   public static void main(String[] args) {  
  8.       
  9.       ApplicationContext  context  = new  ClassPathXmlApplicationContext("resource/others.xml");  
  10.         
  11.  Person  person =   (Person)context.getBean("person");  
  12.         
  13.      // Car  car =   (Car)context.getBean("car");  
  14.         
  15.     //  System.out.println(car);  
  16.     System.out.println(person);  
  17. }  
  18. }  
package com.myapp.core.factorybean;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainTest {
  public static void main(String[] args) {
	
	  ApplicationContext  context  = new  ClassPathXmlApplicationContext("resource/others.xml");
	  
 Person  person =   (Person)context.getBean("person");
	  
	 // Car  car =   (Car)context.getBean("car");
	  
	//  System.out.println(car);
	System.out.println(person);
}
}

测试结果 makeing car::::123
利用FactoryBean创建car成功
只是为了说明思想。因为这个接口太重要了。在Spring中有很多类实现了改接口over

你可能感兴趣的:(factory)