Spring学习笔记--高级特性之FactoryBean

Spring学习笔记–高级特性之FactoryBean

FactoryBean和BeanFactory

BeanFactory接⼝是容器的*接⼝,定义了容器的⼀些基础⾏为,负责⽣产和管理Bean的⼀个⼯⼚,具体使⽤它下⾯的⼦接⼝类型,⽐如ApplicationContext;此处我们重点分析FactoryBean
FactoryBean Spring中Bean有两种,⼀种是普通Bean,⼀种是⼯⼚Bean(FactoryBean),FactoryBean可以⽣成某⼀个类型的Bean实例(返回给我们),也就是说我们可以借助于它⾃定义Bean的创建过程。
Bean创建的三种⽅式中的静态⽅法和实例化⽅法和FactoryBean作⽤类似,FactoryBean使⽤较多,尤其在Spring框架⼀些组件中会使⽤,还有其他框架和Spring框架整合时使⽤

借助FactoryBean可以实现一些复杂bean的创建,首先我们来看FactoryBean接口,如下:

public interface FactoryBean<T> {
    @Nullable
    T getObject() throws Exception;

    @Nullable
    Class<?> getObjectType();

    default boolean isSingleton() {
        return true;
    }
}
  • getObject返回FactoryBean创建的Bean实例对象,如果isSingleton返回true,则该实例会放到Spring容器的单例缓存池当中。
  • getObjectType返回FactoryBean创建的Bean实例对象的类型。
  • isSingleton是否单例

案例

创建实体类Company
public class Company {
    private String name;
    private String address;
    private String scale;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getScale() {
        return scale;
    }

    public void setScale(String scale) {
        this.scale = scale;
    }

    @Override
    public String toString() {
        return "Company{" +
                "name='" + name + '\'' +
                ", address='" + address + '\'' +
                ", scale='" + scale + '\'' +
                '}';
    }
}
借助FactoryBean创建类的实例并且加入到SpringIOC容器管理
public class CompanyFactoryBean implements FactoryBean<Company> {
    private String companyInfo; //公司名称地址规模

    public void setCompanyInfo(String companyInfo) {
        this.companyInfo = companyInfo;
    }

    @Override
    public Company getObject() throws Exception {
        //创建复杂对象Company
        Company company = new Company();
        String[] strings = companyInfo.split(",");
        company.setName(strings[0]);
        company.setAddress(strings[1]);
        company.setScale(strings[2]);
        return company;
    }

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

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

然后再xml文件中定义一个bean

<bean id="companyBean" class="com.lagou.edu.factory.CompanyFactoryBean">
   <property name="companyInfo" value="XXX,中关村,500"/>
</bean>
测试
 @Test
    public void testFactoryBean(){
        ClassPathXmlApplicationContext classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        Company companyBean = (Company) classPathXmlApplicationContext.getBean("companyBean");
        System.out.println(companyBean);
    }

注意:classPathXmlApplicationContext.getBean(“companyBean”);如果我们传入的是("&companyBean")返回的就是CompanyFactoryBean对象。

结果

Spring学习笔记--高级特性之FactoryBean

上一篇:spring源码学习二:FactoryBean的使用


下一篇:FactoryBean工厂