lookup-mehtod和replace-method标签的作用
下面通过一个基于Spring框架的简单示例来解释 lookup - method
和 replace - method
的作用。
1. 项目准备
首先创建一个Maven项目,添加Spring相关的依赖:
<dependencies><dependency><groupId>org.springframework</groupId><artifactId>spring - context</artifactId><version>5.3.10</version></dependency>
</dependencies>
2. 定义接口和实现类
(1)定义一个接口
public interface Shape {void draw();
}
(2)定义接口的实现类
public class Circle implements Shape {@Overridepublic void draw() {System.out.println("Drawing a circle");}
}
(3)定义包含抽象方法的类
public abstract class GraphicEditor {// 抽象方法,用于获取Shape实例public abstract Shape getShape();public void edit() {Shape shape = getShape();shape.draw();}
}
3. 使用 lookup - method
(1)配置Spring XML文件(applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring - beans.xsd"><bean id="circle" class="Circle"/><bean id="graphicEditor" class="GraphicEditor"><!-- lookup - method配置 --><lookup - method name="getShape" bean="circle"/></bean>
</beans>
在上述配置中,<lookup - method>
标签指定了 GraphicEditor
类中的 getShape
方法,每次调用该方法时,Spring容器都会返回 circle
这个 Shape
类型的bean实例。这主要用于解决单例bean在多例场景下的获取问题,确保每次获取到的是一个新的实例。
(2)测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");GraphicEditor editor01 = context.getBean(GraphicEditor.class);GraphicEditor editor02 = context.getBean(GraphicEditor.class);System.out.println(editor01);System.out.println(editor02);editor01.edit();// output:
// com.atguigu.look.GraphicEditor$$EnhancerBySpringCGLIB$$85010a32@6b67034
// com.atguigu.look.GraphicEditor$$EnhancerBySpringCGLIB$$85010a32@6b67034
// Drawing a circle}
}
4. 使用 replace - method
(1)修改 GraphicEditor
类
public class GraphicEditor {public void drawShape() {System.out.println("Drawing a default shape");}public void edit() {drawShape();}
}
(2)定义一个替换方法类
public class ReplaceDrawMethod implements MethodReplacer {public void replaceDrawShape() {System.out.println("Drawing a replaced shape");}@Overridepublic Object reimplement(Object obj, Method method, Object[] args) throws Throwable {replaceDrawShape();return null;}
}
(3)配置Spring XML文件(applicationContext.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"xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring - beans.xsd"><bean id="graphicEditor" class="GraphicEditor"><!-- replace - method配置 --><replaced - method name="drawShape" replacer="replaceDrawMethod"/></bean><bean id="replaceDrawMethod" class="ReplaceDrawMethod"/>
</beans>
在上述配置中,<replaced - method>
标签指定了将 GraphicEditor
类中的 drawShape
方法替换为 ReplaceDrawMethod
类中的 replaceDrawShape
方法。这样,当调用 edit
方法触发 drawShape
时,实际执行的是替换后的方法。
(4)测试代码
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Main {public static void main(String[] args) {ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");GraphicEditor editor = context.getBean(GraphicEditor.class);editor.edit();}
}// output:Drawing a replaced shape
总结
lookup - method
:用于在运行时动态地查找并返回一个bean实例,通常用于解决单例bean需要获取多例bean的场景。replace - method
:用于在运行时替换目标类中的一个方法,提供了一种灵活的方式来修改类的行为,而无需修改原始类的源代码。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/909351.shtml
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!相关文章
个人微信号二次开发API调用、微信API接口
个人微信号二次开发API调用、微信API接口微信API接口、微信二次开发API调用微信协议接口调用-加微信好友及通过好友请求发送小程序
请求URL:
http://域名地址/sendApplets
请求方式:
POST
请求头Headers:
Content-T…
React 学习笔记4 Diffing/脚手架 - 详解
pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …
VisualStudio-Python-工具指南-全-
VisualStudio Python 工具指南(全)原文:zh.annas-archive.org/md5/396df14cf233d147d6dfcb4a589a8b75
译者:飞龙
协议:CC BY-NC-SA 4.0前言
和许多其他开发者一样,Python 开发者总是需要找到方法来管理不同工具之…
深入解析:Spring Boot注解
深入解析:Spring Boot注解2025-09-22 13:14
tlnshuju
阅读(0)
评论(0) 收藏
举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; …
Gevent和Subprocess问题
Gevent和Subprocess问题Gevent和Subprocess问题
1、复现
在main文件中调用gevent、并做了monkey pathch, 然后再调用subprocess.Popen(),出现一直卡住的问题Python 标准库里的很多 IO(网络 socket、ssl、time.sleep…
WPF ListBox loaded 76.6M items with imagesource
Install-Package Microsoft.Extensions.DependencyInjection;
Install-Package CommunityToolkit.mvvm; public async Task InitDataAsync()
{watch.Start();string imgDir = @"../../../Images";if (!Direc…
Ansible自动化运维实战 - 详解
Ansible自动化运维实战 - 详解pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco"…
建筑行业能源管理破局:MyEMS 打造商业楼宇 “能耗可视化 + 智能调控” 方案
在建筑行业的可持续发展浪潮中,商业楼宇正面临着巨大的能源管理压力。高昂的运营成本、日趋严格的碳排政策以及业主与租户对舒适环境日益增长的需求,共同构成了行业必须面对的挑战。传统的能源管理方式往往依赖于人工…
【数据结构】双向链表 - 指南
【数据结构】双向链表 - 指南pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco"…
告别“能源糊涂账”:MyEMS如何帮企业把能耗数据“算明白、用到位”
对于许多企业而言,能源成本是运营中一笔不小的开支,但却常常是一笔“糊涂账”。电费账单只有一个总数,只知道“花了多少钱”,却不知道“钱花在了哪里”、“谁在用”、“什么时候用的”、“有没有浪费”。这种粗放式…
完整教程:ElasticSearch倒排索引原理
pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …
Windows 离线环境下使用 VS Code 连接容器 Python 环境完整指南(亲测可用)
前言
在离线服务器环境中进行 Python 开发时,如何利用 VS Code 的强大功能进行调试和开发是一个常见需求。本文将详细介绍如何在 Windows 离线环境下,通过 WSL 和 Docker 容器实现 VS Code 连接 Python 环境的完整方…
Macos 安装kali报错
执行某个安装步骤失败。您可以尝试从菜单中重新运行这个失败的项目, 或跳过它并选择其他项目。失败的步骤是:选择并安装软件
选择语言的时候选择英文就能安装了。不清楚是什么原因
完整教程:线程、进程、协程
完整教程:线程、进程、协程pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco",…
CF913G Power Substring
推歌:SPOTLIGHT HUNTER
麦晓雯联动出了,没抽到。我爸把我 75 研究卷霍霍露娜上了导致我没法免费保底。诋毁他。
洛谷传送
说回正题。设 \(a\) 有 \(n\) 位,所求的 \(a\) 在 \(2^k\) 中距离末位的位数为 \(m\),显然…
深入解析:老树发新芽:六西格玛培训为石油机械制造注入持久活力
pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …
YC大佬分享的 10 个 vibe coding技巧,看完收获巨大
1、当 AI 卡住的时候切换战场。
问题:
当你在使用 cursor claude code 这样的 AI 编程工具,在编程的时候,如果遇到 AI 无法实现某个功能或者无法修复某一个错误,并且反复尝试都失败(陷入逻辑死循环)
技巧:
这时…