package com.ck.aopTest.aop;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.ck.aopTest.dao.MyAopDao;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.Map;
//执行顺序 环绕前,之前,环绕后,之后,之后@AfterReturning或@AfterThrowing
@Aspect
@Component
public class LogAspect {
private String type;
private String method;
//定义拦截规则
@Pointcut("execution(* get*(..)) || execution(* add*(..))")
private void aop() {
}
@Before("execution(* get*(..))")
public void insert(){
type="1";
}
@Before("execution(* delete*(..))")
public void delete(){
}
@Before("execution(* update*(..))")
public void update(){
}
@Around("aop()")
public Object around(ProceedingJoinPoint joinPoint) {
System.out.println("调用环绕前");
new Date();//开始时间
Object proceed = null;
try {
joinPoint.proceed();
} catch (Throwable e) {
System.out.println("111111");
e.printStackTrace();
}
//pdj.getArgs()获取参数值
//pdj.getSignature().getName() 获取拦截方法
//pdj.getTarget().getClass().getName(); 获取类名
System.out.println("调用环绕后");
return proceed;
}
}
spring.xml
<?xml version="1.0" encoding="UTF-8"?><aop:aspectj-autoproxy />/开启aop注解
<context:component-scan base-package="com.qlzx.aopTest.*"></context:component-scan>
pom.xml
4.0.0 com.qlzx SpringAop 0.0.1-SNAPSHOT<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.0.3.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>javassist</groupId>
<artifactId>javassist</artifactId>
<version>3.12.1.GA</version>
</dependency>