首页 > 其他分享 >spring Bean方法的替换

spring Bean方法的替换

时间:2023-07-30 23:01:01浏览次数:26  
标签:String OldEraPeople spring class Object 替换 Bean 方法 public


具体情况截图

 

spring Bean方法的替换_java

代码如下

OldEraPeople.java

package com.shrimpking.code14;

public class OldEraPeople
{
    public String drink(String name)
    {
        String str = "";
        return str;
    }
}

 NewEraPeople.java

package com.shrimpking.code14;

import org.springframework.beans.factory.support.MethodReplacer;

import java.lang.reflect.Method;

/**
 * @author user1
 */
public class NewEraPeople implements MethodReplacer
{

    @Override
    public Object reimplement(Object obj, Method method, Object[] args) throws Throwable
    {
        String input = (String)args[0];
        System.out.println("传入参数" + input);
        String newStr = input + "新方法替换内容";
        System.out.println(newStr);
        return newStr;
    }
}

beans.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/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <bean id="newPeople" class="com.shrimpking.code14.NewEraPeople"/>

    <bean id="oldPeople" class="com.shrimpking.code14.OldEraPeople">
        <replaced-method name="drink" replacer="newPeople">
            <arg-type>String</arg-type>
        </replaced-method>
    </bean>
</beans>

DemoTest.java

package com.shrimpking.code14;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoTest
{
    @Test
    public void test()
    {
        ApplicationContext context = new ClassPathXmlApplicationContext("/code14/beans.xml");
        OldEraPeople oldEraPeople = context.getBean("oldPeople", OldEraPeople.class);
        oldEraPeople.drink("tom");

    }
}

-----------------------------------------------------------------------------------------------------------------------------

引用:

属性和方法是类的主要组成部分,Spring可以对属性值进行注入配置,也提供了对方法替换的配置。

方法替换配置的使用场景:第三方提供的类方法无法满足应用需求,但是又不想通过反编译改写这个类的方法或此方法还在其他地方使用,就可以配置Bean的replaced-method元素来实现对方法的覆写。

下面以一个实例进行该特性的演示。

有一个类OldEraPeople,包含一个eat()的方法(一般只需要知道该类方法的输入参数和返回类型即可),通过定义一个新的类NewEraPeople,替换旧类的执行方法。

代码如下:

01 public class OldEraPeople { //旧的类定义
02 public String eat(String name){ //旧的类方法,这里仅返回一个空的字串
03 String str = "";
04 return str;
05 }
06 }

新的类需要实现MethodReplacer接口并完成reimplement方法。

代码如下:

//继承MethodReplacer接口的类
01 public class NewEraPeople implements MethodReplacer {
02 @Override //方法重载注解
03 public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
04 String inputParam = (String)args[0];
05 System.out.println("传入参数:"+inputParam);
06 String newStr = inputParam+"在新时代";
07 System.out.println("替换返回新的字符串或对象");
08 return newStr;
09 }
10 }
在XML中增加新类的Bean配置,并且在旧Bean配置中通过元素配置方法替换,replacer是新方法的Bean的ID,name指定需要替换的方法,如下:
01  <bean id="oldEraPeople" class="cn.osxm.ssmi.chp4.methodinj.OldEraPeople">      
<!--方法替换配置 -->
02      <replaced-method name="eat" replacer="newEraPeople">
03          <arg-type>String</arg-type>
04          </replaced-method>
05  </bean>
06  <bean id="newEraPeople" class="cn.osxm.ssmi.chp4.methodinj.NewEraPeople"/>

标签:String,OldEraPeople,spring,class,Object,替换,Bean,方法,public
From: https://blog.51cto.com/u_15356972/6903670

相关文章

  • Java反序列化Commons-Beanutils篇-CB链
    <1>环境介绍jdk:jdk8u65CB:commons-beanutils1.8.3pom.xml添加<dependency><groupId>commons-beanutils</groupId><artifactId>commons-beanutils</artifactId><version>1.8.3</version></dep......
  • Spring系列一:spring的安装与使用
    @目录......
  • Spring Boot学习路线1
    SpringBoot是什么?SpringBoot是基于SpringFramework构建应用程序的框架,SpringFramework是一个广泛使用的用于构建基于Java的企业应用程序的开源框架。SpringBoot旨在使创建独立的、生产级别的Spring应用程序变得容易,您可以"只是运行"这些应用程序。术语SpringCore是Spring......
  • 【Spring Boot 初识丨八 丨外部应用程序属性 】
    上一篇讲了SpringBoot的外部化配置的加载顺序及一些简单的属性说明本篇来讲一讲外部化配置一些比较重要的部分SpringBoot初识:(外部化配置详解)外部应用程序属性  当您的应用程序启动时,SpringBoot将自动从以下位置查找并加载application.properties和application.......
  • SpringBoot 启动流程分析(寻找扩展点)
    1、SpringBootmaven依赖版本<?xmlversion="1.0"encoding="UTF-8"?><projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation=......
  • 通过Redis+Mysql来自定义Spring-Statemachine的持久化
    我们在使用Spring状态机的时候,往往需要对于StateMachine持久化操作,但是官方为我们提供的基于redis的持久化并不是特别好,一方面是因为只存redis容易导致数据丢失,另一方面因为状态机的特性需要对应的StateMachine的数据永久有效,导致redis中的key永不过期。我现在希望实现将StateMac......
  • 02:SpringBoot2 整合 Redis 详细步骤
    1、pom文件中添加redis引用1<dependency>2<groupId>org.springframework.boot</groupId>3<artifactId>spring-boot-starter-web</artifactId>4</dependency>5<dependency>6......
  • 2023Spring project0
    Task1:copy-on-writetrie第一个task实现一个写时复制Trie树,个人理解,这个概念类似于OI中的可持久化Trie树首先大体框架已经给出来了,主要实现三个功能,分别是Get,Put和Remove。Get给定一个key,返回key所对应的value。有以下三种情况:对应的key在Trie树中不存在,那么应该提前退出......
  • 15_Spring_JDBCTemplate批操作
    15_Spring_JDBCTemplate批操作一次连接,操作表格里的多条数据,就是批量操作1批量增加2批量修改3批量删除实体类packagecom.msb.pojo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;importjava.io.Serializable;/**@......
  • 17_Spring_事务环境搭建
    17_Spring_事务环境搭建通过张三给李四转账案例演示事务的控制1数据库中准备表格applicationContext.xmljdbc.properties见上节课2项目中准备实体类packagecom.msb.pojo;importlombok.AllArgsConstructor;importlombok.Data;importlombok.NoArgsConstructor;i......