Spring中的BeanFactory和FactoryBean

BeanFactory是IOC最基本的容器,负责生产和管理bean,它为其它具体的IOC提供了最基本的规范。例如:ApplicationContext等具体的容器,都是实现了BeanFactory,再在其基础上附加了其它的功能。
FactoryBean是一个接口,当在IOC容器中的Bean实现了FactoryBean后,通过getBean(String beanName)获取到的bean对象并不是FactoryBean的实现类对象,而是这个实现类中的getObject()方法返回的对象。要想获取FactoryBean的实现类,就要getBean(&BeanName),在BeanName之前加上&。

FactoryBean可以说为IOC容器中Bean的实现提供了更加灵活的配置,相当于加上了一个简单工厂模式和装饰模式。可以在getObject()方法中灵活配置。

public class FactoryBeanPojo implements FactoryBean{

    private String type;

    @Override
    public Object getObject() throws Exception {
        if ("student".equals(type)){
            return new Student();
        }else {
            return new School();
        }
    }

    @Override
    public Class<?> getObjectType() {
        return School.class;
    }

    @Override
    public boolean isSingleton() {
        return true;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}
public class Application {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("spring-first.xml");
        //测试factoryBean接口
        Object school = context.getBean("factoryBeanPojo");
        FactoryBeanPojo factoryBeanPojo = (FactoryBeanPojo) context.getBean("&factoryBeanPojo");
        System.out.println(school.getClass().getName());
        System.out.println(factoryBeanPojo.getClass().getName());
    }
}


猜你喜欢

转载自blog.csdn.net/chejinqiang/article/details/80038127