我也忘记是从哪里扒来的代码,不过有了这个思路,以后可以自己针对 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. GetStartedWithWin10Develop

    GetStartedWithWin10Develop 首先要确保已经配置好win10开发环境,开始第一个win10开发的HelloWorld 1.首先创建你的win10项目(示例的项目名称为 Hell ...

  2. 手把手搭建自己的android环境

    最近想学习安卓,不过国内实在被墙的厉害,真是"万里安装只被墙".安装的过程中也出现了几个问题.所以记录下来,免得自己下次再次安装的时候又来重蹈覆辙. 以下的问题也是按照出现的顺序排 ...

  3. swift基础一

    // swift中导入类库使用import,不再使用<>和"" import Foundation // 输出 print("Hello, World!&qu ...

  4. 苹果Mac隐藏壁纸在哪里?Mac隐藏壁纸查找教程

    Mac隐藏壁纸怎么查找?Mac存不存在隐藏壁纸呢?今天小编就来解决大家的疑问,告诉大家怎么把Mac系统的隐藏壁纸找出来,并且弄能够正常的使用,小编特意写了一个详细的图文教程叫大家如何查找,使用Mac隐 ...

  5. 拿什么守护你的Node.JS进程: Node出错崩溃了怎么办? foreverjs, 文摘随笔

    守护进程 方案一 npm install forever https://github.com/foreverjs/forever 方案二 npm install -g supervisor http ...

  6. mybatis java.lang.UnsupportedOperationException

    mybatis抛出下面的异常: org.mybatis.spring.MyBatisSystemException: nested exception is org.apache.ibatis.exc ...

  7. ArcGIS API for JavaScript Beta初步试探(一)

    这段时间一直在看https://developers.arcgis.com/javascript/beta/sample-code/index.html, 下面直接看图片: 叠加了二维arcgis s ...

  8. C#操作XML小结(转)

    一.简单介绍 using System.Xml; //初始化一个xml实例 XmlDocument xml=new XmlDocument(); //导入指定xml文件 xml.Load(path); ...

  9. 问题解决——VS2010 将生成的文件复制到指定位置

    我是从VC6直接过渡到VS2010的,VS2008没怎么用过.用VS2010的时候,每次生成dll后,手工把dll.lib..h文件复制到指定文件夹太麻烦了,所以着手写了这个. =========== ...

  10. 【windows环境下】RabbitMq的安装和监控插件安装

    RabbitMq的安装: RabbitMQ是基于Erlang的,所以必须先配置Erlang环境. 下载Erlang,地址:http://www.erlang.org/download/otp_win3 ...