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

Spring 6.0官方文档示例(24): replace-method的用法

一、原始bean定义

package cn.edu.tju.study.service.anno.domain;public class MyValueCalculator {public String computeValue(String input) {return "you inputted: " + input;}// some other methods...
}

二、replace bean定义

package cn.edu.tju.study.service.anno.domain;import org.springframework.beans.factory.support.MethodReplacer;import java.lang.reflect.Method;
import java.util.Locale;public class ReplacementComputeValue implements MethodReplacer {public Object reimplement(Object o, Method m, Object[] args) throws Throwable {String input = (String) args[0];return input.toUpperCase();}
}

三、配置文件

<?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:c="http://www.springframework.org/schema/c"xmlns:p="http://www.springframework.org/schema/p"xsi:schemaLocation="http://www.springframework.org/schema/beanshttps://www.springframework.org/schema/beans/spring-beans.xsd"><bean id="myValueCalculator" class="cn.edu.tju.study.service.anno.domain.MyValueCalculator"><!-- arbitrary method replacement --><replaced-method name="computeValue" replacer="replacementComputeValue"><arg-type>String</arg-type></replaced-method></bean><bean id="replacementComputeValue" class="cn.edu.tju.study.service.anno.domain.ReplacementComputeValue"/></beans>

四、主类:

package cn.edu.tju;import cn.edu.tju.study.service.anno.domain.MyValueCalculator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Start17 {public static void main(String[] args) {ApplicationContext applicationContext = newClassPathXmlApplicationContext("beans17.xml");MyValueCalculator result = applicationContext.getBean("myValueCalculator", MyValueCalculator.class);System.out.println(result.computeValue("hello"));}
}

五、执行结果:
在这里插入图片描述

http://www.lryc.cn/news/131005.html

相关文章:

  • 自然语言处理从入门到应用——LangChain:记忆(Memory)-[聊天消息记录]
  • Python web实战之细说 Django 的单元测试
  • pytorch 42 C#使用onnxruntime部署内置nms的yolov8模型
  • 【Lua】(一)VSCode 搭建 Lua 开发环境
  • react-vite-antd环境下新建项目
  • KeilMDk软仿真设置_STM32F03C8
  • mysql的隐式连接和显式连接的区别
  • vue-element-admin新增view后点击侧边栏加载慢问题
  • 论文《LoRA: Low-Rank Adaptation of Large Language Models》阅读
  • MySQL数据类型篇
  • Eureka注册中心
  • 代码随想录算法训练营第53天|动态规划part14
  • houdini xyzdist primuv 实现按路径走
  • Asrock-Z690-PG-Reptide i5-13600kf电脑 Hackintosh 黑苹果引导文件
  • linux 搭建 nexus maven私服
  • MySQL中按月统计并逐月累加统计值的几种写法
  • 音视频 FFmpeg音视频处理流程
  • Linux网络编程:多进程 多线程_并发服务器
  • 解决:(error) ERR unknown command shutdow,with args beginning with
  • 《TCP IP网络编程》第十八章
  • TCP编程流程
  • CSDN编程题-每日一练(2023-08-19)
  • 03_缓存双写一致性
  • 机器学习之数据集
  • PyTorch Geometric基本教程
  • MAC 命令行启动tomcat的详细介绍
  • idea2023 springboot2.7.5+mybatisplus3.5.2+jsp 初学单表增删改查
  • 轻松搭建书店小程序
  • Spark MLlib机器学习库(一)决策树和随机森林案例详解
  • CI/CD入门(二)