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

spring:继承接口FactoryBean获取bean实例

spring框架提供接口FactoryBean获取bean实例。

实现步骤:

实现接口FactoryBean。

在xml文件中配置实现接口FactoryBean的类。

调用接口FactoryBean中方法getObject,获取bean实例。

 实现接口类

package com.itheima.factory;import org.springframework.beans.factory.FactoryBean;import com.itheima.dao.interfaces.InterfaceUserDao;
import com.itheima.dao.impl.UserDaoImpl;/*** @copyright 2003-2024* @author    qiao wei* @date      2024-12-24* @version   1.0* @brief     继承接口FactoryBean。通过方法getBean获取Bean,时是延迟调用。* @history   name*            date*            brief*/
public class MyBeanFactory03 implements FactoryBean<InterfaceUserDao> {public MyBeanFactory03() {}@Overridepublic InterfaceUserDao getObject() throws Exception {System.out.println("继承接口FactoryBean");return new UserDaoImpl();}@Overridepublic Class<?> getObjectType() {return null;}@Overridepublic boolean isSingleton() {return FactoryBean.super.isSingleton();}
}

xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:p="http://www.springframework.org/schema/p"xmlns:context="http://www.springframework.org/schema/context"xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-4.0.xsd"><!--  --><bean id="myUserDao03"class="com.itheima.factory.MyBeanFactory03"></bean>
</beans>

测试调用

package com.itheima.factory;import org.junit.jupiter.api.Test;
import org.springframework.context.support.ClassPathXmlApplicationContext;import com.itheima.dao.interfaces.InterfaceUserDao;/*** @copyright 2003-2025* @author    qiao wei* @date      2025-04-05* @version   1.0* @brief     继承接口FactoryBean。在读取配置文件时不创建Bean实例。在调用方法getBean时才创建Bean实例并返回。* @history   name*            date*            brief*/
class MyBeanFactory03Test {@Testpublic void test01() {// 读取配置文件,创建容器,未创建Bean实例。ClassPathXmlApplicationContext context =new ClassPathXmlApplicationContext("/xml/factory/myBeanFactory03.xml");// 通过方法getBean获取Bean实例时,才创建Bean实例。InterfaceUserDao userDao = (InterfaceUserDao) context.getBean("myUserDao03");userDao.print();}
}

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

相关文章:

  • Vue速查手册
  • Unity | AmplifyShaderEditor插件基础(第五集:简易膨胀shader)
  • GOOUUU ESP32-S3-CAM 果云科技开发板开发指南(一)(超详细!)Vscode+espidf 通过摄像头拍摄照片并存取到SD卡中,文末附源码
  • SMC自修改代码一
  • JUC 串讲
  • redis分片集群架构
  • 部门档案在不同系统中的差异及整合思路
  • 【自然语言处理】大模型时代的数据标注(主动学习)
  • PostgreSQL 的扩展pageinspect
  • 【Java学习笔记】Math方法
  • Xilinx IP 解析之 Block Memory Generator v8.4 ——02-如何配置 IP(仅 Native 接口)
  • oracle 11g ADG备库报错ORA-00449 lgwr unexpectedly分析处理
  • AGV|无人叉车工业语音播报器|预警提示器LBE-LEX系列性能与接线说明
  • Python从Excel读取数据并生成图表的方法详解
  • Docker构建Vite项目内存溢出:从Heap Limit报错到完美解决的剖析
  • LINUX67 FTP 3客户服务系统;FTP 上传、下载测试调试
  • 14-Oracle 23ai Vector Search 向量索引和混合索引-实操
  • [Go]context上下文--使用要点--源码分析--Go核心--并发编程
  • go-zero微服务入门案例
  • 【Go语言基础【13】】函数、闭包、方法
  • 优化器 (torch.optim) 与学习率调度器 (lr_scheduler)
  • vite+tailwind封装组件库
  • Android LinearLayout、FrameLayout、RelativeLayout、ConstraintLayout大混战
  • Xela矩阵三轴触觉传感器的工作原理解析与应用场景
  • 一.设计模式的基本概念
  • Python分形几何可视化—— 复数迭代、L系统与生物分形模拟
  • Redis专题-基础篇
  • Vue具名插槽
  • Linux(13)——Ext系列文件系统
  • Now formdata是什么?如何使用