Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
There are two types of Spring Bean, Bean one is common, i.e., the other is FactoryBean Bean plant
factory and Bean Common Bean different, the object is not specified it returns an instance of the class, which is returned to the plant Bean getObject method
Sample code is as follows:
//先准备一个Address类
public class Address {
private String city;
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Address(String city) {
super();
this.city = city;
}
public Address() {
}
@Override
public String toString() {
return "Address [city=" + city + "]";
}
}
//FactoryBean工厂Bean
public class AddressBeanFactory implements FactoryBean<Address>{
private String city;
public void setCity(String city) {
this.city = city;
}
//FactoryBean返回的实例
@Override
public Address getObject() throws Exception {
return new Address(this.city);
}
//FactoryBean返回的类型
@Override
public Class<?> getObjectType() {
return Address.class;
}
//FactoryBean返回的实例是否为单例
@Override
public boolean isSingleton() {
return true;
}
}