Spring的方法替换例子

Spring xml

 

   
       
   

   
   

 

Java 调用

public class MockClass implements MockInterface {

    public void execute() {
        System.out.println("in execute");
    }

    public ValueBean getValueBean(String name){
        return null;
    }
   
    public ValueBean invokeGetValueBean(String name){
        return getValueBean(name);
    }
}

 

public class MethodReplaceTest {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("test/method/method-context.xml");
        MockClass mc = (MockClass)ctx.getBean("mockClass");
       
        System.out.println("---------------Invoke directly----------------");
        ValueBean vb = mc.getValueBean("june");
        System.out.println(vb);
       
        System.out.println("---------------Invoke indirectly----------------");
        vb = mc.invokeGetValueBean("june");
        System.out.println(vb);
    }
}

 

输出结果

---------------Invoke directly----------------
june
---------------Invoke indirectly----------------
june

 

证明即使间接调用,也同样可以被replace

 

 

你可能感兴趣的:(Spring)