欢迎转载交流:个人博客地址http://www.cnblogs.com/shizhongtao/p/3468713.html

org.springframework.beans.factory.support.MethodReplacer这个接口作用是替换方法时候用的。就是执行时候用新建的逻辑替换已有的方法逻辑。具体使用实例如下:

新建类:

 public class MvcService
{
public String getTime(){
SimpleDateFormat formate=new SimpleDateFormat("yy-MM-dd");
return formate.format(new Date());
}
}

然后创建替代的类及方法:

 import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Calendar; import org.springframework.beans.factory.support.MethodReplacer; public class MvcServiceReplaceImpl implements MethodReplacer{ @Override
public Object reimplement(Object arg0, Method arg1, Object[] arg2)
throws Throwable {
SimpleDateFormat formate=new SimpleDateFormat("yy-MM-dd HH:mm:ss.SS");
Calendar c=Calendar.getInstance();
c.add(Calendar.YEAR, 2);
return formate.format(c.getTime());
} }

配置文件加入:

 <bean id="mvcService" class="com.bing.service.MvcService">
<replaced-method name="getTime" replacer="replacementComputeValue">
<!-- <arg-type>String</arg-type> -->
</replaced-method> </bean>
<bean id="replacementComputeValue" class="com.bing.service.MvcServiceReplaceImpl" />

这里的意思是用MvcServiceReplaceImpl中的方法替代类com.bing.service.MvcService中的getTime方法。当你运行时候返回的结果就不是上面的“yy-MM-dd”的时间格式

而是"yy-MM-dd HH:mm:ss.SS"的时间格式

当然方法替代实现方式应该很多,比如,我们使用aop方式,在aspectj中使用@Around标签时候会用一个类被注入进来-ProceedingJoinPoint,它继承了JoinPoint接口,这个接口可以获得所有参数签名与值,然后执行自己所要的操作就好了,最后如果有返回就return就好了。(但是目前没有考虑这个调用是不是会有其他问题,因为还米有来得及看源码)

简介spring中MethodReplacer的用法的更多相关文章

  1. 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)

    一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...

  2. Spring中@Cacheable的用法

    在Spring中通过获取MemCachedClient来实现与memcached服务器进行数据读取的方式.不过,在实际开发中,我们往往是通过Spring的@Cacheable来实现数据的缓存的,所以, ...

  3. Spring中HibernateCallback的用法(转)

    Hibernate的复杂用法HibernateCallback HibernateTemplate还提供一种更加灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式.Hiber ...

  4. Spring中jdbcTemplate的用法实例

    一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象.       第一种方式:我们可以在自己定 ...

  5. Spring 中classPath:用法

    参考文章地址: http://hi.baidu.com/huahua035/item/ac8a27a994b55bad29ce9d39 http://blog.csdn.net/lushuaiyin/ ...

  6. spring 中StoredProcedure的用法--转载

    StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate执行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...

  7. Spring中ApplicationContextAware的用法

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt379 一.这个接口有什么用? 当一个类实现了这个接口(Application ...

  8. Spring中RedirectAttributes的用法

    RedirectAttributes 是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的的工具类.他有两种带参的方式: 第一种: redirectAttribut ...

  9. spring中ApplicationListener的用法

    1.实现ApplicationListener接口,并重写onApplicationEvent方法 @Component public class RSAKeyInitListener impleme ...

随机推荐

  1. Js Pattern - Self Define Function

    This pattern is useful when your function has some initial preparatory work to do andit needs to do ...

  2. Codeforces Round #250 (Div. 1) A. The Child and Toy 水题

    A. The Child and Toy Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/438/ ...

  3. Type mytableview does not confirm to portocol UITableViewDataResource

    继承UITableViewDataSource报上面这个总是,是重写协议时写错了 override func numberOfRowsInSection(section: Int) -> Int ...

  4. vs2012调试时为什么会出现clr.dll与mscordacwks.dll版本不匹配

    第一次开发wpf程序,就遇到了上述问题,最后发现是因为vs2012默认用的是.net4.5,将版本改为.net4就好了,问题解决

  5. 嵌入式系统 Boot Loader 技术内幕

    转载:http://www.ibm.com/developerworks/cn/linux/l-btloader/index.html   1. 引言 在专用的嵌入式板子运行 GNU/Linux 系统 ...

  6. do {...} while (0) in macros

    If you are a C programmer, you must be familiar with macros. They are powerful and can help you ease ...

  7. 一个JS版本的MD5

    var hexcase = 0; function hex_md5(a) { if (a == "") return a; return rstr2hex(rstr_md5(str ...

  8. oracle锁

    1.概念 数据库中有两种基本的锁类型:排它锁(Exclusive Locks,即X锁)和共享锁(Share Locks,即S锁). 当数据对象被加上排它锁时,其他的事务不能对它读取和修改:加了共享锁的 ...

  9. Linux系统上安装软件(JDK以及tomcat服务器)

    一:安装jdk linux系统上面如果运行java程序,就需要安装java的运行环境(jdk) 1:下载linux版本的jdk 地址:http://www.oracle.com/technetwork ...

  10. Linux系统调用(转载)

    目录: 1. Linux系统调用原理 2. 系统调用的实现 3. Linux系统调用分类及列表 4.系统调用.用户编程接口(API).系统命令和内核函数的关系 5. Linux系统调用实例 6. Li ...