Explain FactoryBean in DETAIL
- 2020-06-19 10:12:56
- OfStack
spring FactoryBean is to create complex bean, the 1-like bean can be configured directly with xml, if the creation of an bean involves many other bean and complex logic, it is difficult to configure with xml, then FactoryBean can be considered
Examples are as follows:
1: Create 1 Car class (for simplicity) 1 Car class cannot be given directly, it can be injected directly or Car object, just for simplicity.
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:1 FactoryBean implementations have created car
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;
}
}
Creating car above is too easy, if it is too simple, there is no need to create it with FactoryBean, you can write more complicated.
3: Person references 1 car
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: Configure reference xml format:
<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: Write tests:
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);
}
}
Test results makeing car::::123
Using FactoryBean to create car successfully
Just to illustrate the idea. Because this interface is so important. There are many classes in Spring that implement this interface.