xml-配置bean之replaced-method

siye@r480:~/svlution/workspace/springcore4322$ tree src/
src/
├── main
│   ├── java
│   │   ├── log4j.properties
│   │   └── ocn
│   │       └── site
│   │           └── springioc
│   │               ├── domain
│   │               │   └── User.java
│   │               └── utils
│   │                   └── MethodReplacerImpl.java
│   └── resources
└── test
    ├── java
    │   └── ocn
    │       └── site
    │           └── springioc
    │               └── domain
    │                   └── Runtest.java
    └── resources
        └── config
            └── application.xml

16 directories, 5 files
<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.22.RELEASE</version>
</dependency>
<!-- https://mvnrepository.com/artifact/log4j/log4j -->
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.17</version>
</dependency>
<!-- https://mvnrepository.com/artifact/junit/junit -->
<dependency>
    <groupId>junit</groupId>
    <artifactId>junit</artifactId>
    <version>4.12</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.springframework/spring-test -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-test</artifactId>
    <version>4.3.22.RELEASE</version>
    <scope>test</scope>
</dependency>
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;

public class User {

	private final Logger logger = Logger.getLogger(this.getClass());

	public String getInfo(String arg) {
		logger.info("orginal method info");
		return "null";
	}

}
package ocn.site.springioc.utils;

import java.lang.reflect.Method;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.support.MethodReplacer;

public class MethodReplacerImpl implements MethodReplacer {

	private final Logger logger = Logger.getLogger(this.getClass());

//  参数列表说明:
//  obj : 表示的需要替换方法的目标类实例;
//  method : 需要替换的方法签名;
//  args : 需要替换的方法的参数列表;
//  return : 自定义该方法的返回值;注意类型要和目标类方法返回类型一致.
	@Override
	public Object reimplement(Object obj, Method method, Object[] args) throws Throwable {
		logger.info("replace method info");
		return "success";
	}

}
<?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">

	<!--
		方法替换:
		即替换目标类中的任意方法;
		适用的场景:有时候需要引入的外部类无法满足现有的需由,但是又不能更改依外部类的属性和行为;
		此时使用spring ioc中的方法替换机制,即可满足需求,降低对外部类的侵入性.

		目前版本中,只能实现指定的类,来满足这一需求,即实现 org.springframework.beans.factory.support.MethodReplacer
		接口.
		然后结合<bean>的内置标签<replaced-method>实现功能.

		使用注意事项:
		a)<replaced-method>标签,应当内嵌在目标类所属的<bean>标签内;
		<bean id="targetCls" class="">
		<replaced-method name="replaceMethodName" replacer="replaceClsIdName">
		<arg-type match=""/>
		</replaced-method>
		</bean>

		b)<replaced-method>标签属性的说明:
		name:指的是需要替换的方法的签名;
		replacer:指的是 MethodReplacer 的实现类的bean的id或name名称;
		内嵌标签<agr-type>的说明:指的是替换方法的参数类型,若是多个参数,按照参数的次序,可依次指明参数类型;
		其中的属性`match`的值应当定义类型的完全限定名的值;

		c)MethodReplacer接口方法的说明:
		Object reimplement(Object obj, Method method, Object[] args) throws Throwable;
		参数列表:
		obj : 表示的需要替换方法的目标类实例;
		method : 需要替换的方法签名;
		args : 需要替换的方法的参数列表;
		返回类型 Object
		return : 自定义该方法的返回值;注意类型要和目标类方法返回类型一致.
	-->
	<bean id="replaceBean" class="ocn.site.springioc.utils.MethodReplacerImpl"></bean>
	<bean class="ocn.site.springioc.domain.User">
		<replaced-method name="getInfo" replacer="replaceBean">
			<arg-type match="java.lang.String"></arg-type>
		</replaced-method>
	</bean>

</beans>
package ocn.site.springioc.domain;

import org.apache.log4j.Logger;
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.SpringRunner;

@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:config/application.xml")
public class Runtest {

	private final Logger logger = Logger.getLogger(this.getClass());
	private @Autowired User user;

	@Test
	public void run() throws Exception {
		String info = user.getInfo("");
		logger.info(info);
	}

}
19-09-08 17:33:33 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Loaded default TestExecutionListener class names from location [META-INF/spring.factories]: [org.springframework.test.context.web.ServletTestExecutionListener, org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener, org.springframework.test.context.support.DependencyInjectionTestExecutionListener, org.springframework.test.context.support.DirtiesContextTestExecutionListener, org.springframework.test.context.transaction.TransactionalTestExecutionListener, org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]
19-09-08 17:33:33 org.springframework.test.context.support.DefaultTestContextBootstrapper  =====>>> Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4e04a765, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@783e6358, org.springframework.test.context.support.DirtiesContextTestExecutionListener@17550481]
19-09-08 17:33:33 org.springframework.beans.factory.xml.XmlBeanDefinitionReader  =====>>> Loading XML bean definitions from class path resource [config/application.xml]
19-09-08 17:33:33 org.springframework.context.support.GenericApplicationContext  =====>>> Refreshing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 17:33:33 CST 2019]; root of context hierarchy
19-09-08 17:33:33 ocn.site.springioc.utils.MethodReplacerImpl  =====>>> replace method info
19-09-08 17:33:33 ocn.site.springioc.domain.Runtest  =====>>> success
19-09-08 17:33:33 org.springframework.context.support.GenericApplicationContext  =====>>> Closing org.springframework.context.support.GenericApplicationContext@2ef5e5e3: startup date [Sun Sep 08 17:33:33 CST 2019]; root of context hierarchy
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值