spring关于FactoryBean的基本介绍

FactoryBean是什么

  1. FactoryBean简介:

Spring中Bean有两种,⼀种是普通Bean,⼀种是⼯⼚Bean(FactoryBean),FactoryBean可以⽣成 某⼀个类型的Bean实例(返回给我们),也就是说我们可以借助于它⾃定义Bean的创建过程。

接口源码

// 可以让我们⾃定义Bean的创建过程(完成复杂Bean的定义)
public interface FactoryBean<T> {
     @Nullable
     // 返回FactoryBean创建的Bean实例,如果isSingleton返回true,则该实例会放到Spring容器
    的单例对象缓存池中Map
     T getObject() throws Exception;
     @Nullable
     // 返回FactoryBean创建的Bean类型
     Class<?> getObjectType();
     // 返回作⽤域是否单例
     default boolean isSingleton() {
     return true;
     }
}
  1. BeanFactory 和 FactoryBean的区别

    • BeanFactory接⼝是容器的顶级接⼝,定义了容器的⼀些基础⾏为,负责⽣产和管理Bean的⼀个⼯⼚, 具体使⽤它下⾯的子接⼝类型,比如ApplicationContext;
    • FactoryBean 可以自定义生产bean的过程,然后获得对应的bean对象
  2. FactoryBean 和 Bean的静态方法和实例化方法创建

    Bean创建的三种⽅式中的静态⽅法和实例化⽅法和FactoryBean作⽤类似,FactoryBean使⽤较多,尤其在Spring框架⼀些组件中会使用,还有其他框架和Spring框架整合时使⽤

FactoryBean的使用步骤

  1. 导入springIOC的pom依赖

    <!--引入Spring IoC容器功能-->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.1.12.RELEASE</version>
    </dependency>
    
  2. 创建bean的类 和 生产对应bean的FactoryBean(需要实现FactoryBean接口)。

    Computer类:

    package com.lb.demo;
    
    public class Computer {
    
        private String cpu;
    
        private String ram;
    
        private String ssd;
    
        public String getCpu() {
            return cpu;
        }
    
        public void setCpu(String cpu) {
            this.cpu = cpu;
        }
    
        public String getRam() {
            return ram;
        }
    
        public void setRam(String ram) {
            this.ram = ram;
        }
    
        public String getSsd() {
            return ssd;
        }
    
        public void setSsd(String ssd) {
            this.ssd = ssd;
        }
    
        @Override
        public String toString() {
            return "Computer{" +
                    "cpu='" + cpu + '\'' +
                    ", ram='" + ram + '\'' +
                    ", ssd='" + ssd + '\'' +
                    '}';
        }
    }
    
    

    ComputerFactoryBean类:

    package com.lb.demo;
    
    import org.springframework.beans.factory.FactoryBean;
    
    public class ComputerFactoryBean implements FactoryBean<Computer> {
    
        private String computerInfo;
    
        public void setComputerInfo(String computerInfo) {
            this.computerInfo = computerInfo;
        }
    
        @Override
        public Computer getObject() throws Exception {
    
            //自定义创建bean对象的过程
            Computer computer = new Computer();
            String[] split = computerInfo.split(",");
            computer.setCpu(split[0]);
            computer.setRam(split[1]);
            computer.setSsd(split[2]);
            return computer;
        }
    
        @Override
        public Class<?> getObjectType() {
            //返回对象类型
            return Computer.class;
        }
    
    }
    
    
  3. 创建applicationContext.xml文件,添加bean对象的标签,class标明当前bean对应的BeanFactory的全限定类名即可。

    <?xml version="1.0" encoding="UTF-8"?>
    <beans default-lazy-init="true"
           xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
     https://www.springframework.org/schema/beans/spring-beans.xsd">
    
        <bean id="computer" class="com.lb.demo.ComputerFactoryBean">
            <property name="computerInfo" value="i7型号,16G-RAM,512G-SSD"></property>
        </bean>
    
    </beans>
    
  4. 获取bean,直接从容器中调用getBean方法,即会从FactoryBean中调用getObject方法获取对应的bean对象。若想获取FactoryBean,需要在getBean的id前面添加 ‘&’ 符号

    • 获取bean对象

              ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
              Computer computer = (Computer) applicationContext.getBean("computer");
              System.out.println(computer);
      

      测试结果:

      请添加图片描述

    • 获取FactoryBean对象

              ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
              ComputerFactoryBean computerFactoryBean = (ComputerFactoryBean) applicationContext.getBean("&computer");
              System.out.println(computerFactoryBean);
      

      测试结果:

      请添加图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值