方法替换及在不改变原方法的情况下,使用新的方法去替换原有的方法。以下例子来演示通过XML配置的方式来替换已经存在方法。

使用技术及工具:

1.eclipse 4.4.2
2.Maven 3.1
3.JDK 1.7
4.Spring 4.3.5
5.Junit 4.12

Pom.xml配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.devnp</groupId>
<artifactId>Spring4Demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Spring4Demo</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.3.5.RELEASE</spring.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>

原方法类:

HelloService.java

1
2
3
4
5
6
7
8
package com.devnp.SpringDemo;

public class HelloService {

public void print(String str){
System.out.println("Print:" + str);
}
}

替换方法类:

MethodReplacementDemo.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.devnp.SpringDemo.method.replace;

import java.lang.reflect.Method;

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

public class MethodReplacementDemo implements MethodReplacer{

public Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws Throwable {

String inputStr = (String) arg2[0];

System.out.println("This is replace Method Print :" + inputStr);

return null;
}


}

Spring XML 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?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:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd">


<bean id="helloService" class="com.devnp.SpringDemo.HelloService">
<replaced-method name="print" replacer="methodReplacementDemo">
<!-- if need, we can base on the arg type -->
<!-- <arg-type>String</arg-type> -->
</replaced-method>
</bean>

<bean id="methodReplacementDemo" class="com.devnp.SpringDemo.method.replace.MethodReplacementDemo"/>

</beans>

测试类:

MethodReplacementDemoTest.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
package com.devnp.SpringDemo.method.replace;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import com.devnp.SpringDemo.HelloService;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:ApplicationContext.xml")
public class MethodReplacementDemoTest {

@Autowired
private HelloService helloService ;

@Test
public void test1(){
helloService.print("Hello World");
}

}

输入结果:

This is replace Method Print :Hello World

代码下载:

Spring4Demo.zip