FactoryBean是什么
- 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;
}
}
-
BeanFactory 和 FactoryBean的区别
- BeanFactory接⼝是容器的顶级接⼝,定义了容器的⼀些基础⾏为,负责⽣产和管理Bean的⼀个⼯⼚, 具体使⽤它下⾯的子接⼝类型,比如ApplicationContext;
- FactoryBean 可以自定义生产bean的过程,然后获得对应的bean对象
-
FactoryBean 和 Bean的静态方法和实例化方法创建
Bean创建的三种⽅式中的静态⽅法和实例化⽅法和FactoryBean作⽤类似,FactoryBean使⽤较多,尤其在Spring框架⼀些组件中会使用,还有其他框架和Spring框架整合时使⽤
FactoryBean的使用步骤
-
导入springIOC的pom依赖
<!--引入Spring IoC容器功能--> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>5.1.12.RELEASE</version> </dependency>
-
创建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; } }
-
创建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>
-
获取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);
测试结果:
-