我也忘记是从哪里扒来的代码,不过有了这个思路,以后可以自己针对 Controller 还有 Service层的任意 方法进行代理了

package pw.jonwinters.aop;
import java.lang.annotation.*; /**
*自定义注解 拦截Controller
*/ @Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemControllerLog {
String description() default "";
}
package pw.jonwinters.aop;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; @Target({ElementType.PARAMETER, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface SystemServiceLog {
String description() default "";
}
package pw.jonwinters.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; import java.lang.reflect.Method; import javax.servlet.http.HttpServletRequest; import pw.jonwinters.service.*;
@Aspect
@Component
public class SystemLogAspect {
@Autowired
@Qualifier("baseLogger")
private Logger log; @Pointcut("@annotation(pw.jonwinters.aop.SystemServiceLog)") //拦截带有此类注解的方法
public void serviceAspect() {
}
@Pointcut("@annotation(pw.jonwinters.aop.SystemControllerLog)")
public void controllerAspect() {
} @Before("controllerAspect()")
public void doBefore(JoinPoint joinPoint) throws Throwable { //joinPoint
log.info("aop is running!");
log.info("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
log.info("方法描述:" + getControllerMethodDescription(joinPoint)); //请求的IP
Thread.sleep(20000);
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String ip = request.getRemoteAddr();
try {
//*========控制台输出=========*//
log.info("=====前置通知开始=====");
log.info("请求方法:" + (joinPoint.getTarget().getClass().getName() + "." + joinPoint.getSignature().getName() + "()"));
log.info("方法描述:" + getControllerMethodDescription(joinPoint));
log.info("请求IP:" + ip);
// Thread.sleep(2000);
}
finally{ }
} @AfterThrowing(pointcut = "serviceAspect()", throwing = "e")
public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { } }
public  static String getControllerMethodDescription(JoinPoint joinPoint)  throws Exception {
String targetName = joinPoint.getTarget().getClass().getName(); //这是个工具类, 通过joinPoint对象 获得目标对象的引用 获得拦截对象的Class对象,然后获得Class对象的名称
String methodName = joinPoint.getSignature().getName(); //获得拦截方法的名称
Object[] arguments = joinPoint.getArgs(); //获得拦截方法的参数对象数组
Class<?> targetClass = Class.forName(targetName); //获得拦截对象的Class
Method[] methods = targetClass.getMethods(); //获得拦截对象的所有方法
String description = "";
for (Method method : methods) { //遍历方法
if (method.getName().equals(methodName)) { //当此方法的名称与拦截方法的名称相同时候
Class[] clazzs = method.getParameterTypes(); //获得这个方法的所有参数类型的 Class对象
if (clazzs.length == arguments.length) { //确保拦截的方法的参数个数 与 解析到的参数个数相同
description = method.getAnnotation(SystemControllerLog.class).description(); //然后获得方法的注解的description字段的值 并且返回
break;
}
}
}
return description;
}

Spring AOP 针对注解的AOP的更多相关文章

  1. Spring Boot 自定义注解,AOP 切面统一打印出入参请求日志

    其实,小哈在之前就出过一篇关于如何使用 AOP 切面统一打印请求日志的文章,那为什么还要再出一篇呢?没东西写了? 哈哈,当然不是!原因是当时的实现方案还是存在缺陷的,原因如下: 不够灵活,由于是以所有 ...

  2. Spring AspectJ基于注解的AOP实现

    对于AOP这种编程思想,很多框架都进行了实现.Spring就是其中之一,可以完成面向切面编程.然而,AspectJ也实现了AOP的功能,且实现方式更为简捷,使用更加方便,而且还支持注解式开发.所以,S ...

  3. spring中基于注解使用AOP

    本文内容:spring中如何使用注解实现面向切面编程,以及如何使用自定义注解. 一个场景 比如用户登录,每个请求发起之前都会判断用户是否登录,如果每个请求都去判断一次,那就重复地做了很多事情,只要是有 ...

  4. spring的AOP——采用注解完成AOP

    AOP的两种配置方式:XML配置和Aspectj注解方式. 一.项目的目录: 二.文件配置 我们采用的是JDK代理,所以首先将接口和实现类代码附上: public interface UserMana ...

  5. Spring Boot使用注解实现AOP

    第一步: 添加依赖 <dependency> <groupId>org.springframework.boot</groupId> <artifactId& ...

  6. Spring的AOP机制---- AOP的注解配置---- AOP的注解配置

    3333隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约隐隐约约噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢噢 ...

  7. Spring之注解实现aop(面向切面编程)

    1:Aop(aspect object programming)面向切面编程,名词解释:    1.1:功能:让关注点代码与业务逻辑代码分离    1.2:关注点        重复代码就叫做关注点  ...

  8. 深入理解Spring Redis的使用 (六)、用Spring Aop 实现注解Dao层的自动Spring Redis缓存

    摘要: 主要针对Dao层的一些数据库查询的操作,数据实时性不强,直接加入缓存.当缓存中有的时候,就使用缓存中的数据.这样的方法,最终仅仅使用一个注解实现.对于之前的hibernate二级缓存使用,比较 ...

  9. spring源码学习之AOP(一)

    继续源码学习,看了spring中基础的容器和AOP感觉自己也没有什么长进,哈哈,我也不知道到底有用没有,这可能是培养自己的一种精神吧,不管那么多,继续学习!AOP中 AOP中几个重要的概念:(1)Ad ...

随机推荐

  1. Postman的使用

    在我们平时开发中,特别是需要与接口打交道时,无论是写接口还是用接口,拿到接口后肯定都得提前测试一下,这样的话就非常需要有一个比较给力的Http请求模拟工具,现在流行的这种工具也挺多的,像火狐浏览器插件 ...

  2. c++类的定义《一》

    最近好忙,一来要在店里看店,二来朋友办结婚酒,搞的我这几天好疲惫啊···博客又有好几天没提笔了. 下午简单看了下书,看到了类的部分,自己动手练习了一下 笔记:1.类是数据类型 / 它的变童就是对象  ...

  3. SharePoint 2013 调用WCF服务简单示例

    内容比较简单,主要记录自己使用SharePoint 2013WCF服务遇到的小问题和小经验,分享给大家,希望能够给需要的人有所帮助.好吧,进入正题! 第一部分 SharePoint 2013调用自带W ...

  4. 2015年第8本(英文第7本):the city of ember 微光城市

    书名:the City of Ember(中文名:微光城市) 作者:Jeanne DuPrau 单词数:6.2万 不重复单词数:未知 首万词不重复单词数:未知 蓝思值:未知 阅读时间:2015年4月2 ...

  5. UC如被百度控股,手机qq浏览器改如何进攻和防守

    很早以前在公司内部论坛里写的一篇文章,绯闻已经过过去了,现在已物事人物,UC已有阿里大靠山了. ----------------------------------------------- 据网络媒 ...

  6. 初始block,关于定义的几个小题目

    block的定义和C语言指针函数非常相似,就可以照着指针函数的方法去依葫芦画瓢就可以了 block中的^只是用来表示这是一个block对象,和函数指针中的*作用一样,只是一个标识符 下面有三个小例子来 ...

  7. IOS之UI -- UITableView -- 1 -- 相关初识

    *:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...

  8. IOS之未解问题--关于IOS图像渲染CPU和GPU

    前言:先上一个图.关于UIKit底层渲染机制,这个有待以后花大把时间收集资料和学习,然后汇总,将整篇"未解问题"去除. 反思:半个月前阿里电话面试,问道一个问题图像渲染什么时候回用 ...

  9. 配置JDK环境变量

    •配置JDK环境变量<Windows系统下> 点击我的电脑右键----->属性------>高级------>环境变量------->  新建(建议在系统变量中新建 ...

  10. Linux网络编程&内核学习

    c语言: 基础篇 1.<写给大家看的C语言书(第2版)> 原书名: Absolute Beginner's Guide to C (2nd Edition) 原出版社: Sams 作者: ...