码迷,mamicode.com
首页 > 其他好文 > 详细

invoke 使代码更加结构清晰

时间:2015-01-08 15:02:21      阅读:208      评论:0      收藏:0      [点我收藏+]

标签:

不多说,直接贴。

public static Map<String,String> urlMthod = new HashMap<String,String>();
static {
urlMthod.put("/user/add.xhtm", "add");

}

这个方法中转器,就是将每个切进来的方法,指向它要处理的方法中;这样写避免了很多if else的方法,而且结构更加清晰,对于某段的逻辑的修改删除很方便。

public void dataStatisticBefore(JoinPoint joinPoint) {
  Long currentTime = System.currentTimeMillis();
  Object[] objects = joinPoint.getArgs();
  for(Object object : objects ) {
    if(object instanceof HttpServletRequest) {
      HttpServletRequest sr = (HttpServletRequest) object;
      String url = (String) sr.getAttribute((URL_MAPPING));
      String methodName = urlMthod.get(url);
      try {
        this.getClass()
          .getMethod(
          methodName,new Class[] { String.class,HttpServletRequest.class,Long.class,String.class})
          .invoke(this, new Object[] {url,sr,currentTime,methodName});
      } catch (Exception e) {
        logDataStatistic.error("aop dataStatistic_Around : " + url + " error :", e);
      }
    break;
    }
  }
}

 

public void add(...) {

}

invoke 使代码更加结构清晰

标签:

原文地址:http://www.cnblogs.com/codermaster/p/4210630.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!