zoukankan      html  css  js  c++  java
  • 简介spring中MethodReplacer的用法

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

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

    新建类:

    1 public class MvcService
    2 {
    3     public String getTime(){
    4         SimpleDateFormat formate=new SimpleDateFormat("yy-MM-dd");
    5         return formate.format(new Date());
    6     }
    7 }

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

     1 import java.lang.reflect.Method;
     2 import java.text.SimpleDateFormat;
     3 import java.util.Calendar;
     4 
     5 import org.springframework.beans.factory.support.MethodReplacer;
     6 
     7 public class MvcServiceReplaceImpl implements MethodReplacer{
     8 
     9     
    10 
    11     @Override
    12     public Object reimplement(Object arg0, Method arg1, Object[] arg2)
    13             throws Throwable {
    14         SimpleDateFormat formate=new SimpleDateFormat("yy-MM-dd HH:mm:ss.SS");
    15         Calendar c=Calendar.getInstance();
    16         c.add(Calendar.YEAR, 2);
    17         return formate.format(c.getTime());
    18     }
    19 
    20 }

    配置文件加入:

    1 <bean id="mvcService" class="com.bing.service.MvcService">
    2         <replaced-method name="getTime" replacer="replacementComputeValue">
    3             <!-- <arg-type>String</arg-type> -->
    4         </replaced-method>
    5 
    6     </bean>
    7     <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就好了。(但是目前没有考虑这个调用是不是会有其他问题,因为还米有来得及看源码)

  • 相关阅读:
    js 的防抖与节流
    Vue---图形二维码、rules校验规则、el-dialog展示图片
    vue ----弹框
    vue的背景加载--旋转*号
    利用ES6新特性将时间戳转换为yyyy-mm-dd格式
    路由守卫之离开守卫
    Java的运行环境与开发环境
    list<map<string,object>> 按照某字段排序
    没有CSRF保护的HTML表单 漏洞解决办法
    通过mybatis-plus的分页插件,实现分页
  • 原文地址:https://www.cnblogs.com/shizhongtao/p/3468713.html
Copyright © 2011-2022 走看看