示例来自 汪云飞的 《Spring boot 实战》, 重点是看 LogAspect.java 上面有我的注释.
@Before("annotationPointCut()") // 4 这是使用@xxxxx的方式拦截,具体到这里就是所有标注为@Action 的方法都会被拦截
@Before("execution(*com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") // 5 这是使用拦截规则被拦截,具体到这里就是指所有com.wisely.highlight_spring4.ch1.aop.DemoMethodService里面的方法都会被拦截
这两种貌似只能两者选其一
具体代码如下
com.wisely.highlight_spring4.ch1.aop.Main
1st. @interface
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Action {
String asd();
}
2nd. @Aspect
package com.wisely.highlight_spring4.ch1.aop;
import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
@Aspect// 1
@Component// 2
public class LogAspect {
@Pointcut("@annotation(com.wisely.highlight_spring4.ch1.aop.Action)") // 3
public void annotationPointCut() {
};
// @Before("annotationPointCut()") // 4 这是使用@xxxxx的方式拦截,具体到这里就是所有标注为@Action 的方法都会被拦截
@Before("execution(*com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") // 5 这是使用拦截规则被拦截,具体到这里就是指所有com.wisely.highlight_spring4.ch1.aop.DemoMethodService里面的方法都会被拦截
public void before(JoinPoint joinPoint) {
MethodSignaturesignature = (MethodSignature) joinPoint.getSignature();
Methodmethod =signature.getMethod();
Actionaction =method.getAnnotation(Action.class);
// 如果使用 5 行的方式拦截,则action将会为null, action.asd()将会抛出异常
System.out.println("before方法规则式拦截, 被拦截的方法 ="
+ method.getName()
+ ", 标注中属性asd的值 = " + action.asd()
+ ", 被拦截的对象的包名 = " + signature.getDeclaringTypeName());
}
// @After("annotationPointCut()") //6 这是使用@xxxxx的方式拦截,具体到这里就是所有标注为@Action 的方法都会被拦截
@After("execution(*com.wisely.highlight_spring4.ch1.aop.DemoMethodService.*(..))") // 7 这是使用拦截规则被拦截,具体到这里就是指所有com.wisely.highlight_spring4.ch1.aop.DemoMethodService里面的方法都会被拦截
public void after(JoinPoint joinPoint) {
MethodSignaturesignature = (MethodSignature) joinPoint.getSignature();
Methodmethod =signature.getMethod();
// 如果使用 7 行的方式拦截,则action将会为null
Actionaction =method.getAnnotation(Action.class);
System.out.println("after方法规则式拦截, 被拦截的方法 = "
+ method.getName()
+ ", method.getAnnotation(Action.class) =" + (null==action ? "null" : "非null")
+ ", 被拦截的对象的包名 = " + signature.getDeclaringTypeName());
}
}
3rd. Main
package com.wisely.highlight_spring4.ch1.aop;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
AnnotationConfigApplicationContext context =
new AnnotationConfigApplicationContext(AopConfig.class); //1
DemoAnnotationService demoAnnotationService = context.getBean(DemoAnnotationService.class);
DemoMethodService demoMethodService = context.getBean(DemoMethodService.class);
// demoAnnotationService.add();
//
// demoAnnotationService.delete();
demoMethodService.add();
context.close();
}
}
4th. @Configuration
package com.wisely.highlight_spring4.ch1.aop;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@ComponentScan("com.wisely.highlight_spring4.ch1.aop")
@EnableAspectJAutoProxy//1
public class AopConfig {
}
5th. Annotatiuon标记的被拦截类
package com.wisely.highlight_spring4.ch1.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoAnnotationService {
@Action(asd = "注解式拦截的add操作aaaaaaa1111")
public void add() {
System.out.println("有标注 DemoAnnotationService add 方法被调用了!");
}
@Action(asd="注解式拦截的delete操作bbbb1111")
public void delete(){
System.out.println("有标注 DemoAnnotationService delete 方法被调用了!");
}
}
6th. 正则表达式定义的被拦截类
package com.wisely.highlight_spring4.ch1.aop;
import org.springframework.stereotype.Service;
@Service
public class DemoMethodService {
public void add(){
System.out.println("没有标注 DemoMethodService add 方法被调用了!");
}
}