简介spring中MethodReplacer的用法
欢迎转载交流:个人博客地址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的用法的更多相关文章
- 框架源码系列十:Spring AOP(AOP的核心概念回顾、Spring中AOP的用法、Spring AOP 源码学习)
一.AOP的核心概念回顾 https://docs.spring.io/spring/docs/5.1.3.RELEASE/spring-framework-reference/core.html#a ...
- Spring中@Cacheable的用法
在Spring中通过获取MemCachedClient来实现与memcached服务器进行数据读取的方式.不过,在实际开发中,我们往往是通过Spring的@Cacheable来实现数据的缓存的,所以, ...
- Spring中HibernateCallback的用法(转)
Hibernate的复杂用法HibernateCallback HibernateTemplate还提供一种更加灵活的方式来操作数据库,通过这种方式可以完全使用Hibernate的操作方式.Hiber ...
- Spring中jdbcTemplate的用法实例
一.首先配置JdbcTemplate: 要使用Jdbctemplate 对象来完成jdbc 操作.通常情况下,有三种种方式得到JdbcTemplate 对象. 第一种方式:我们可以在自己定 ...
- Spring 中classPath:用法
参考文章地址: http://hi.baidu.com/huahua035/item/ac8a27a994b55bad29ce9d39 http://blog.csdn.net/lushuaiyin/ ...
- spring 中StoredProcedure的用法--转载
StoredProcedure是一个抽象类,必须写一个子类来继承它,这个类是用来简化JDBCTemplate执行存储过程操作的. 首先我们写一个实现类: package com.huaye.frame ...
- Spring中ApplicationContextAware的用法
详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt379 一.这个接口有什么用? 当一个类实现了这个接口(Application ...
- Spring中RedirectAttributes的用法
RedirectAttributes 是Spring mvc 3.1版本之后出来的一个功能,专门用于重定向之后还能带参数跳转的的工具类.他有两种带参的方式: 第一种: redirectAttribut ...
- spring中ApplicationListener的用法
1.实现ApplicationListener接口,并重写onApplicationEvent方法 @Component public class RSAKeyInitListener impleme ...
随机推荐
- Swift学习笔记四
前面三篇笔记通过一些示例展示了Swift的一些特性,粗略地介绍了它的语法和特色,从这一篇笔记开始,将正式系统地介绍Swift的语法和特性了. Swift是一门为iOS和OSX开发准备的全新语言,但是它 ...
- 异常捕捉 ( try catch finally ) 你真的掌握了吗?
前言:java 中的异常处理机制你真的理解了吗?掌握了吗?catch 体里遇到 return 是怎么处理? finally 体遇到 return 怎么办?finally 体里有 System.exit ...
- Delphi调用Dll的的2种写法
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, F ...
- WIN8重见开始菜单
从win7进入win8感觉不适应,做为一个程序开发人员,更觉得不爽,因此想着如何恢复开始菜单,查了很多文章终于找到方法 首先,在桌面版Windows 8启动资源管理器,单击工具栏上的“查看”选项卡后, ...
- PHP实现微博的同步发送(转)
导读:在设计博客类站点时,有时会需要在发布文章时同步发布在微博上.本文阐述了实现该功能的基本方式. 准备工作 作为新浪微博的开发者,需要有身份验证: 个人身份认证的审核,一般一个工作日: 接着是提 ...
- Innode引擎监控的开启的方法
查看当前InnoDB引擎信息 mysql> show innodb status\G 开启InnoDB监控,有四种: 1. innodb_monitor mysql> create tab ...
- Google Code Jam 2009, Round 1C C. Bribe the Prisoners (记忆化dp)
Problem In a kingdom there are prison cells (numbered 1 to P) built to form a straight line segment. ...
- Android(java)学习笔记114:LayoutInflater和findViewById
1. 在实际开发中LayoutInflater这个类还是非常有用的,它的作用类似于findViewById(). 不同点是LayoutInflater是用来找res/layout/下的xml布局文件, ...
- android开发之路09(浅谈SQLite数据库01)
1.SQLite数据库: SQLite 是一个开源的嵌入式关系数据库,实现自包容.零配置.支持事务的SQL数据库引擎. 其特点是高度便携.使 用方便.结构紧凑.高效.可靠. 与其他数据库管理系统不同, ...
- 《sift算法详解》阅读笔记
原博客来自:http://blog.csdn.net/zddblog/article/details/7521424 定义: 尺度不变特征转化是一种计算机视觉算法,用于侦测和描述物体的局部性特征,在空 ...