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();
    }
}

三、配置文件




    
        
        
            String
        
    
    





四、主类:

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 = new
                ClassPathXmlApplicationContext("beans17.xml");
        MyValueCalculator result = applicationContext.getBean("myValueCalculator", MyValueCalculator.class);
        System.out.println(result.computeValue("hello"));

    }
}

五、执行结果:
Spring 6.0官方文档示例(24): replace-method的用法_第1张图片

你可能感兴趣的:(Spring,spring,java,后端)