实现FactoryBean接口
(1)编写一个类实现FactoryBean接口
public class MyFactoryBeanImpl implements FactoryBean<AirPlane> {
@Override
public AirPlane getObject() throws Exception {
AirPlane airPlane=new AirPlane();
airPlane.setAirName("爱国者");
airPlane.setCustomerNumber(300);
airPlane.setColor("blue");
return airPlane;
}
@Override
public Class<?> getObjectType() {
return AirPlane.class;
}
@Override
public boolean isSingleton() {
return false;
}
}
public class AirPlane {
private String captainName;
private Integer customerNumber;
private String airName;
private String color;
public AirPlane() {
}
public void setCaptainName(String captainName) {
this.captainName = captainName;
}
public void setCustomerNumber(Integer customerNumber) {
this.customerNumber = customerNumber;
}
public void setAirName(String airName) {
this.airName = airName;
}
public void setColor(String color) {
this.color = color;
}
@Override
public String toString() {
return "AirPlane{" +
"captainName='" + captainName + '\'' +
", customerNumber=" + customerNumber +
", airName='" + airName + '\'' +
", color='" + color + '\'' +
'}';
}
}
(2)配置文件
<bean id="airPlane3" class="com.orz.spring.bean.MyFactoryBeanImpl" />
(3)测试
@Test
public void test1()
{
ApplicationContext applicationContext=new ClassPathXmlApplicationContext("bean.xml");
AirPlane airPlane = applicationContext.getBean("airPlane3", AirPlane.class);
System.out.println(airPlane);
}
(4)结果
AirPlane{captainName='null', customerNumber=300, airName='爱国者', color='blue'}