当前位置: 首页 > news >正文

FactoryBean接口作用

         FactoryBean是一个生产Bean产品的工厂,它是一个接口,只要实现了这个接口就可以生产Bean.

package com.xuecheng.content.a01;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.stereotype.Component;/*** @author zhou* @version 1.0* @description TODO* @date 2025/8/27 22:29*/
@Slf4j
@Component("bean1")
public class MyFactoryBean implements FactoryBean {//提供产品对象@Overridepublic Object getObject() throws Exception {Bean1 bean1 = new Bean1();log.debug("creating bean: {}",bean1);return bean1;}//决定了根据类型获取或依赖注入能否成功@Overridepublic Class<?> getObjectType() {return Bean1.class;}@Overridepublic boolean isSingleton() {//单例return true;}
}

实现这个接口需要重写上面三个方法。

1.getObject()返回实体

2.getObjectType()返回实体的类型

3.isSingleton()决定生产的Bean对象是否是单例

对FactoryBean类进行测试:

首先创建Bean1

package com.xuecheng.content.a01;import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.BeanFactoryAware;
import org.springframework.beans.factory.annotation.Autowired;import javax.annotation.PostConstruct;/*** @author zhou* @version 1.0* @description TODO* @date 2025/8/27 22:30*/
@Slf4j
public class Bean1 implements BeanFactoryAware {private Bean2 bean2;@Autowiredpublic void setBean2(Bean2 bean2){this.bean2 = bean2;}public Bean2 getBean2(){return bean2;}@PostConstructpublic void init(){log.debug("init");}@Overridepublic void setBeanFactory(BeanFactory beanFactory) throws BeansException {log.debug("setBeanFactory{}",beanFactory);}
}

         Bean1(没有被Spring管理)里面依赖了Bean2,并把Bean2交给Spring管理,方便后期测试,同时实现BeanFactoryAware 接口,设置Bean1的BeanFactory。

package com.xuecheng.content.a01;import org.springframework.stereotype.Component;/*** @author zhou* @version 1.0* @description TODO* @date 2025/8/27 22:35*/
@Component
public class Bean2 {
}

package com.xuecheng.content.a01;import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;/*** @author zhou* @version 1.0* @description TODO* @date 2025/8/27 22:49*/
@ComponentScan
public class TestFactoryBean {public static void main(String[] args) {AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(TestFactoryBean.class);Bean1 bean1 = (Bean1) context.getBean("bean1");System.out.println(bean1);context.close();}
}

        把测试类交给Spring管理,我们通过容器获取名字为bean1的bean。容器从BeanFactory里面拿到了bean1。

测试结果:

            Bean1被创建,但是没有做Bean1中写的一些初始化的操作。

http://www.xdnf.cn/news/1377541.html

相关文章:

  • 使用Stone 3D快速制作第一人称视角在线小游戏
  • 【PyTorch】基于YOLO的多目标检测项目(二)
  • 基于Cursor AI IDE的Vue3留言板系统实战:从零搭建到智能优化全流程
  • 《金融对账系统雪崩隐患的深度复盘与架构重生》
  • 从CTFshow-pwn入门-pwn40理解64位栈溢出不都需要堆栈平衡
  • 致远OA新闻公告讨论调查信息查询SQL
  • Linux操作系统——TCP服务端并发模型
  • 域名、ip、DSN、URL
  • 虚拟机逃逸攻防演练
  • 装饰器模式(C++python)
  • 如何提升素材检索效率?语义搜索在 DAM 中的应用效果全解
  • 广东省省考备考(第八十八天8.27)——判断推理(听课后强化训练)
  • 基于NXP iMXRT600音频算法开发方法
  • 【ros-humble】【虚拟机】网络配置
  • 【leetcode】105. 从前序与中序遍历序列构造二叉树
  • 机器视觉学习-day05-图片颜色识别及颜色替换
  • 指针 (六):sizeof和strlen细节强化之“做题篇”
  • 深度学习:常用的损失函数的使用
  • Python随机选择完全指南:从基础到高级工程实践
  • 数据库:缓冲池和磁盘I/O
  • FPGA入门学习路径
  • 【Python 提高】GUI 界面 Tkinter 库布局管理器 Pack 方法开发指南
  • 树的常见算法及Java实现
  • 【yocto】Yocto Project 核心:深入了解.inc文件
  • Java循环结构全解析
  • android 嵌套webview 全屏展示 页面延伸到状态栏且不被底部导航栏遮挡
  • 高并发内存池(11)-PageCache获取Span(下)
  • 【C++标准库】<ios>详解基于流的 I/O
  • 腾讯云 CVM 上的 SpringBoot 应用避免非法访问
  • 寄存器的原理