刚才学习SpringAop的时候觉得好强大
演示效果
1 2 3
| docker-run curl http://localhost:9003/aop/
{"code":"200"}%
|
1 2
| [XNIO-1 task-1] [LogsAspect.java : 50] annotationAop value index [XNIO-1 task-1] [LogsAspect.java : 50] annotationAop value index
|
Spring Aop
AOP(Aspect-Oriented Programming:⾯向切⾯编程)能够将那些与业务⽆关,却为业务模块所共同调⽤的逻辑或责任(例如事务处理、⽇志管理、权限控制等)封装起来,便于减少系统的重复代码,降低模块间的耦合度,并有利于未来的可拓展性和可维护性。
- Spring Aop是运行时增强。
- Cglib Aop是编译器增强。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
| @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Import(AspectJAutoProxyRegistrar.class) public @interface EnableAspectJAutoProxy {
boolean proxyTargetClass() default false;
boolean exposeProxy() default false;
}
|
Spring注解
1 2 3 4 5 6 7
| @Aspect @Pointcut @Before @AfterReturning @Afterthrowing @After @Around
|
pom依赖
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
|
代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
| package cn.z201.aop;
import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;
@Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface AnnotationAop {
String value();
}
package cn.z201.aop;
import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.AfterThrowing; 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;
import java.lang.reflect.Method;
@Component @Aspect @Slf4j public class LogsAspect {
@Pointcut("@annotation(cn.z201.aop.AnnotationAop)") private void cutMethod() {
}
@AfterThrowing("cutMethod()") public void afterThrowing(JoinPoint joinPoint) { log.info("after throwing"); }
@Before("cutMethod()") public void before(JoinPoint joinPoint) throws Throwable { String methodName = joinPoint.getSignature().getName(); Object[] params = joinPoint.getArgs(); if (null != params && params.length != 0) { log.info(" method name {} args {}", methodName, params[0]); } AnnotationAop annotationAop = getDeclaredAnnotation(joinPoint); if (null != annotationAop) { log.info(" annotationAop value {} ", annotationAop.value()); } }
public AnnotationAop getDeclaredAnnotation(JoinPoint joinPoint) throws NoSuchMethodException { String methodName = joinPoint.getSignature().getName(); Class<?> targetClass = joinPoint.getTarget().getClass(); Class<?>[] parameterTypes = ((MethodSignature) joinPoint.getSignature()).getParameterTypes(); Method objMethod = targetClass.getMethod(methodName, parameterTypes); AnnotationAop annotation = objMethod.getDeclaredAnnotation(AnnotationAop.class); return annotation; }
}
package cn.z201.aop;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap; import java.util.Map;
@RestController public class AppApplicationController {
@RequestMapping(value = "") @AnnotationAop(value = "index") public Object index() { Map<String, Object> data = new HashMap<>(); data.put("code", "200"); return data; } }
|
END