1、首先引入 切面 依赖
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.2</version>
</dependency>
2、如果是基于 自定义注解的 切面,需要 编写一个自定义注解类
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface LogAnnotation {
String value();
}
3、基于自定义注解的 切面实现
@Aspect
@Order(-10)
@Component
public class LogAspect {
@Before("@annotation(logAnnotation)")
public void beforePrintLog(JoinPoint joinPoint, LogAnnotation logAnnotation){
ServletRequestAttributes attributes = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
HttpServletRequest request = attributes.getRequest();
System.out.println("================ before =================");
System.out.println("before");
System.out.println(joinPoint);
System.out.println(logAnnotation.value());
System.out.println("================ before =================");
}
@Before("@annotation(logAnnotation)")
public void afterPrintLog(JoinPoint joinPoint, LogAnnotation logAnnotation){
System.out.println("================ after =================");
System.out.println(joinPoint);
System.out.println(logAnnotation.value());
System.out.println("================ after =================");
}
@Pointcut("execution(* com.hwy.study01.controller.*.*(..))")
public void pcut1(){}
@Pointcut("execution(* com.hwy.study01.controller.UserController.*(..))")
public void pcut2(){}
@Before("LogAspect.pcut1() || pcut2()")
public void beforeController(JoinPoint point) {
System.out.println("================ pcut after =================");
System.out.println(point.getSignature().getDeclaringType());
System.out.println("================ pcut after =================");
}
}
4、使用方法
@LogAnnotation("添加")
public String add() {
return "add";
}
@LogAnnotation("更新")
public String update() {
return "update";
}
需要了解几个概念
-
自定义注解
-
常用的元注解有( @Target, @Retention, @Decomented, @Inherited)
-
@Target注解,是专门用来限定某个自定义注解能够被应用在哪些Java元素上面的
public enum ElementType {
/** 类,接口(包括注解类型)或枚举的声明 */
TYPE,
/** 属性的声明 */
FIELD,
/** 方法的声明 */
METHOD,
/** 方法形式参数声明 */
PARAMETER,
/** 构造方法的声明 */
CONSTRUCTOR,
/** 局部变量声明 */
LOCAL_VARIABLE,
/** 注解类型声明 */
ANNOTATION_TYPE,
/** 包的声明 */
PACKAGE
}
-
@Retention注解,翻译为持久力、保持力。即用来修饰自定义注解的生命力
public enum RetentionPolicy {
/**
* Annotations are to be discarded by the compiler.
* (注解将被编译器忽略掉)
*/
SOURCE,
/**
* Annotations are to be recorded in the class file by the compiler
* but need not be retained by the VM at run time. This is the default
* behavior.
* (注解将被编译器记录在class文件中,但在运行时不会被虚拟机保留,这是一个默认的行为)
*/
CLASS,
/**
* Annotations are to be recorded in the class file by the compiler and
* retained by the VM at run time, so they may be read reflectively.
* (注解将被编译器记录在class文件中,而且在运行时会被虚拟机保留,因此它们能通过反射被读取到)
*/
RUNTIME
}
-
@Documented注解,是被用来指定自定义注解是否能随着被定义的java文件生成到JavaDoc文档当中。
-
@Inherited注解,是指定某个自定义注解如果写在了父类的声明部分,那么子类的声明部分也能自动拥有该注解。@Inherited注解只对那些@Target被定义为ElementType.TYPE的自定义注解起作用
AOP 中 execution 详细介绍
例: execution (* com.sample.service..*. *(..))
整个表达式可以分为五个部分:
1、execution()::表达式主体。
2、第一个*号:表示返回类型, *号表示所有的类型。
3、包名:表示需要拦截的包名,后面的两个句点表示当前包和当前包的所有子包,com.sample.service包、子孙包下所有类的方法。
4、第二个*号:表示类名,*号表示所有的类。
5、*(..):最后这个星号表示方法名,*号表示所有的方法,后面括弧里面表示方法的参数,两个句点表示任何参数