目录
一、BeanFactory
二、FactoryBean
三、小结
Spring学习专栏
1. Spring源码分析(一)基本框架介绍
2. Spring源码分析(二)两种方案搭建基础框架
3. Spring中@Conditional注解详细讲解及示例
4. Spring中@Import注解详细讲解及示例
5. Spring中BeanFactory和FactoryBean区别
这个其实是所有Spring Bean的容器根接口,给Spring 的容器定义一套规范,给IOC容器提供了一套完整的规范,比如我们常用到的getBean方法等
The root interface for accessing a Spring bean container. This is the basic client view of a bean container
定义方法
主要的实现类(包括抽象类)
使用方式:
1. 使用ClassPathXmlApplicationContext读取对应的xml文件实例对应上下文对象
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");
User user = applicationContext.getBean("user", User.class);
2.使用AnnotationConfigApplicationContext读取@Bean对应上下文对象
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanFactoryConfig.class);
Person person = applicationContext.getBean("person", Person.class);
该类是SpringIOC容器是创建Bean的一种形式,这种方式创建Bean会有加成方式,融合了简单的工厂设计模式于装饰器模式
/*nterface to be implemented by objects used within a {@link BeanFactory} which
* are themselves factories for individual objects. If a bean implements this
* interface, it is used as a factory for an object to expose, not directly as a
* bean instance that will be exposed itself.*/
有些人就要问了,我直接使用Spring默认方式创建Bean不香么,为啥还要用FactoryBean做啥,在某些情况下,对于实例Bean对象比较复杂的情况下,
使用传统方式创建bean会比较复杂,例如(使用xml配置),这样就出现了FactoryBean接口,可以让用户通过实现该接口来自定义该Bean接口的实例化过程。
即包装一层,将复杂的初始化过程包装,让调用者无需关系具体实现细节。
方法:
常用类:
1. 创建 Person类
public class Person {
private String userName;
private Integer age;
public Person() { }
public Person(String userName, Integer age) {
this.userName = userName;
this.age = age;
}
@Override
public String toString() {
return "Person{" +
"userName='" + userName + '\'' +
", age='" + age + '\'' +
'}';
}
public void setUserName(String userName) {
this.userName = userName;
}
public void setAge(Integer age) {
this.age = age;
}
}
2. 创建 MyFactoryBean类实现FactoryBean
public class MyFactoryBean implements FactoryBean {
private String initStr;
public MyFactoryBean(String initStr) {
this.initStr = initStr;
}
/**
* 获取Bean返回对象
* @date: 2021/2/26 11:35
* @return: java.lang.Object
*/
public Object getObject() throws Exception {
//这里我需要获取对应参数
String[] split = initStr.split(",");
Person person =new Person();
person.setAge(Integer.parseInt(split[0]));
person.setUserName(split[1]);
return person;
}
public Class getObjectType() {
return Person.class;
}
public boolean isSingleton() {
return true;
}
public String getInitStr() {
return initStr;
}
}
3. 创建 BeanFactoryConfig类
@Configuration
public class BeanFactoryConfig {
@Bean(name = "person")
protected Person getBillInfo() {
return new Person("bill", 45);
}
@Bean("myFactoryBean")
protected MyFactoryBean getMyFactoryBean() {
return new MyFactoryBean("30,init from factory");
}
}
4. 创建 BeanFactoryTest测试类
public class BeanFactoryTest {
private static AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(BeanFactoryConfig.class);
@Test
public void test1(){
Person person = applicationContext.getBean("person", Person.class);
System.out.println(person);
//获取从工厂Bean中获取对象
Person myFactoryBean = applicationContext.getBean("myFactoryBean", Person.class);
System.out.println(myFactoryBean);
//获取对应的personFactory
MyFactoryBean factoryBean = applicationContext.getBean("&myFactoryBean",MyFactoryBean.class);
System.out.println("初始化参数为:"+factoryBean.getInitStr());
}
}
控制台打印结果输出
项目地址
https://gitee.com/gaibianzlp/spring-study-demo.git
参考文档
1. BeanFactory 简介以及它 和FactoryBean的区别(阿里面试)
感谢大家看到最后,如文章有不足,欢迎大家在评论区支持,给予意见。如果觉得我的文章对你有帮助,那就给我一个赞同吧。