org.springframework.boot spring-boot-starter-aop
- package com.nanfeng.aop;
-
- import org.aspectj.lang.ProceedingJoinPoint;
- import org.aspectj.lang.annotation.Around;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Component;
-
- import java.util.Arrays;
-
- @Aspect
- @Component
- public class AopAdvice {
-
- private final static Logger logger = LoggerFactory.getLogger(AopAdvice.class);
-
-
- /*
- 注意: PointCut中可以使用 && 、|| 、! 运算
- */
-
- /**
- * 自定义注解 :凡是被注解标注的方法和类都会处罚aop
- */
- @Pointcut("@annotation(com.nanfeng.aop.AopTag)")
- public void annotation() {
- }
-
-
- /**
- * 表达式:访问修饰符 返回值 包名.包名.包名…类名.方法名(参数列表)
- * 标准写法 :public void com.nanfeng.aop.service.AopService.executionAop()
- * 省略修饰符号:void com.nanfeng.aop.service.AopService.executionAop()
- * 返回值使用通配符: * com.nanfeng.aop.service.AopService.executionAop()
- * 通配符标表示: *.*.*.AopService.*()
- * 多包名匹配: *.*.*.AopService.*()
- * 参数类型匹配(根据类型匹配): *.*.*.AopService.*(int,double)
- * 参数通配: *.*.*.AopService.*(..)
- */
- @Pointcut("execution(* com.nanfeng.aop.service.AopService.aopExecution(..))")
- public void execution1() {
- }
-
-
- /**
- * 指定类型中的所有方法将被拦截
- * 不包含子类,只是针对当前对象
- * 包名通配方式 ..
- * 类名通配方式*
- */
- @Pointcut("within(com.nanfeng.aop.service.WithService)")
- public void with() {
- }
-
- /***
- * 类型匹配,包含子类中的所有方法(子类中扩展的方法也包含在其中)
- */
- @Pointcut("this(com.nanfeng.aop.service.ThisService)")
- public void thisService() {
- }
-
-
- /***
- * 类型匹配,包含子类中的所有方法(子类中扩展的方法也包含在其中)
- */
- @Pointcut("target(com.nanfeng.aop.service.TargetService)")
- public void targetService() {
- }
-
-
- /**
- * args() 匹配不带参数的方法
- * args(java.lang.String) 匹配方法参数是String类型的
- * ==args(…) == 带任意参数的方法
- * args(java.lang.String,…) 匹配第一个参数是String类型的,其他参数任意。最后一个参数是String的同理。
- */
- @Pointcut("args(..)")
- public void argsService() {
- }
-
- /**
- * 匹配方法带有参数com.nanfeng.aop.AopTag注解的方法
- */
- @Pointcut("@args(com.nanfeng.aop.AopTag)")
- public void argAnnotationsService() {
- }
-
- /**
- * 匹配bean为beanService的所有方法
- */
- @Pointcut("bean(beanService)")
- public void beanService() {
- }
-
-
- @Around("annotation()")
- public Object annotationAdvice(ProceedingJoinPoint pjp) throws Throwable {
- Object[] args = pjp.getArgs();
- Object result = pjp.proceed(args);
- System.out.println("annotation");
- return result;
- }
-
- @Around("execution1()")
- public Object executionAdvice1(ProceedingJoinPoint pjp) {
- Object[] args = pjp.getArgs();
- System.out.println(Arrays.toString(args));
- System.out.println("execution");
- Object result = null;
- try {
- result = pjp.proceed(args);
- } catch (Throwable e) {
- throw new RuntimeException(e);
- }
- return result;
- }
-
- @Around("with()")
- public Object withAdvice(ProceedingJoinPoint pjp) {
- Object[] args = pjp.getArgs();
- System.out.println(Arrays.toString(args));
- System.out.println("with");
- Object result = null;
- try {
- result = pjp.proceed(args);
- } catch (Throwable e) {
- throw new RuntimeException(e);
- }
- return result;
- }
-
- @Around("thisService()")
- public Object thisAdvice(ProceedingJoinPoint pjp) throws Throwable {
- //获取方法的信息
- MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
- String name = methodSignature.getMethod().getName();
- logger.info("thisService");
- //然后做一些重大操作 todo :
- System.out.println("method name is " + name);
- Object[] args = pjp.getArgs();
- System.out.println(Arrays.toString(args));
- Object result = pjp.proceed(args);
- return result;
- }
-
- @Around("targetService()")
- public Object targetAdvice(ProceedingJoinPoint pjp) throws Throwable {
- //获取方法的信息
- MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
- String name = methodSignature.getMethod().getName();
- logger.info(" targetService");
- //然后做一些重大操作 todo...................
- System.out.println("method name is " + name);
- Object[] args = pjp.getArgs();
- System.out.println(Arrays.toString(args));
- Object result = pjp.proceed(args);
- return result;
- }
-
- @Around("beanService())")
- public Object beanAdvice(ProceedingJoinPoint pjp) throws Throwable {
- //获取方法的信息
- MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
- String name = methodSignature.getMethod().getName();
- logger.info("beanService");
- //然后做一些重大操作 todo...................
- System.out.println("method name is " + name);
- Object[] args = pjp.getArgs();
- System.out.println(Arrays.toString(args));
- Object result = pjp.proceed(args);
- return result;
- }
- }
3 配合通知类的service以及注解:
- package com.nanfeng.aop;
-
- import java.lang.annotation.ElementType;
- import java.lang.annotation.Retention;
- import java.lang.annotation.RetentionPolicy;
- import java.lang.annotation.Target;
-
- @Retention(RetentionPolicy.RUNTIME)
- @Target({ElementType.METHOD, ElementType.TYPE})
- public @interface AopTag {
- String value() default "";
- }
- package com.nanfeng.aop.service;
-
- import com.alibaba.fastjson.JSONObject;
- import com.nanfeng.aop.AopTag;
- import org.springframework.stereotype.Service;
-
- @Service
- public class AopService {
- public Object aopExecution(Object o) {
- return JSONObject.toJSONString(o);
-
- }
-
- @AopTag(value = "cccc")
- public Object annotation1(Object o) {
- return JSONObject.toJSONString(o);
- }
- }
- package com.nanfeng.aop.service;
-
- import org.springframework.stereotype.Component;
-
- @Component
- public class BeanService {
- public void bean1() {
- System.out.println("bean");
- }
- public void bean2() {
- System.out.println("bean");
- }
- }
- package com.nanfeng.aop.service;
-
- public interface TargetService {
- public Object target1(Object o);
- }
- package com.nanfeng.aop.service;
-
- import com.alibaba.fastjson.JSONObject;
- import org.springframework.stereotype.Service;
-
- @Service
- public class TargetServiceImp1 implements TargetService{
- @Override
- public Object target1(Object o) {
- return JSONObject.toJSONString(o);
- }
- }
- package com.nanfeng.aop.service;
-
- import com.alibaba.fastjson.JSONObject;
- import org.springframework.stereotype.Service;
-
- @Service
- public class TargetServiceImp2 implements TargetService {
- @Override
- public Object target1(Object o) {
- return JSONObject.toJSONString(o);
- }
-
-
- public Object target22(Object o) {
- return JSONObject.toJSONString(o);
- }
- }
- package com.nanfeng.aop.service;
-
- public interface ThisService {
- public String AopThis();
- }
- package com.nanfeng.aop.service;
-
- import org.springframework.stereotype.Component;
-
- @Component
- public class ThisServiceImp implements ThisService{
- @Override
- public String AopThis() {
- return "this aop service";
- }
- }
- package com.nanfeng.aop.service;
-
- import org.springframework.stereotype.Component;
-
- @Component
- public class ThisServiceImp1 implements ThisService{
- @Override
- public String AopThis() {
- return "this aop service1";
- }
-
- public String AopThis1() {
- return "this aop service2";
- }
- }
- package com.nanfeng.aop.service;
-
- import org.springframework.stereotype.Service;
-
- @Service
- public class WithService {
- public String aopwith() {
- return "31213";
- }
- }
- package com.nanfeng.aop;
-
- import com.nanfeng.aop.service.*;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestBody;
- import org.springframework.web.bind.annotation.RestController;
-
- @RestController
- public class AopController {
-
- @Autowired
- private AopService service;
-
- @Autowired
- private WithService withService;
-
- @Autowired
- private ThisServiceImp thisService;
- @Autowired
- private ThisServiceImp1 thisService1;
-
-
- @Autowired
- private TargetServiceImp1 target1;
- @Autowired
- private TargetServiceImp2 target2;
-
- @Autowired
- private BeanService beanService;
-
-
- @PostMapping("execution")
- public Object execution(@RequestBody Object o) {
- return service.aopExecution(o);
- }
-
- @PostMapping("annotation")
- public Object annotation1(@RequestBody Object o) {
- return service.annotation1(o);
- }
-
- @PostMapping("with")
- public Object with(@RequestBody Object o) {
- return withService.aopwith();
- }
-
- @PostMapping("this")
- public Object thisaop(@RequestBody Object o) {
- return thisService.AopThis() + thisService1.AopThis() + thisService1.AopThis1();
- }
-
- @PostMapping("target")
- public Object targetaop(@RequestBody Object o) {
- return target1.target1(o).toString() + target2.target1(o).toString() + target2.target22(o).toString();
- }
-
- @PostMapping("bean")
- public Object beanaop(@RequestBody Object o) {
- beanService.bean1();
- beanService.bean2();
- return o ;
- }
- }