标签:ott github 了解 user 方式 表达式 bar target 展示
面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术。AOP是OOP的延续,是软件开发中的一个热点,也是Spring框架中的一个重要内容,是函数式编程的一种衍生范型。利用AOP可以对业务逻辑的各个部分进行隔离,从而使得业务逻辑各部分之间的耦合度降低,提高程序的可重用性,同时提高了开发的效率。
我们现在做的一些非业务,如:日志、事务、安全等都会写在业务代码中(也即是说,这些非业务类横切于业务类),但这些代码往往是重复,复制——粘贴式的代码会给程序的维护带来不便,AOP就实现了把这些业务需求与系统需求分开来做。这种解决的方式也称代理机制。
先来了解一下AOP的相关概念,《Spring参考手册》中定义了以下几个AOP的重要概念,结合下面示例分析如下:
通知(Advice)类型:
接下来通过一下例子来演示SpringCloud+aop
1 <!--引用AOP注解功能开始--> 2 <dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter-aop</artifactId> 5 </dependency> 6 <!--引用AOP注解功能结束-->
public enum LogType { INFO, WARN, ERROR }
1 /** 2 * 系统日志记录 3 * 4 * @author cjg 5 * 6 */ 7 @Target({ ElementType.METHOD }) 8 @Retention(RetentionPolicy.RUNTIME) 9 @Documented 10 public @interface ServiceLog { 11 /** 12 * 操作类型,新增用户?删除用户 ?调用xx服务?使用接口?... 13 * 14 * @return 15 */ 16 public String operation(); 17 18 /** 19 * 日志级别 20 * 21 * @return 22 */ 23 public LogType level() default LogType.INFO; 24 25 }
1 @Component 2 @Aspect 3 public class LogAspect { 4 5 private static final Logger log = LoggerFactory.getLogger(LogAspect.class); 6 7 /** 8 * 切入点 9 */ 10 @Pointcut("@annotation(com.didispace.web.aspect.ServiceLog) ") 11 public void entryPoint() { 12 // 无需内容 13 } 14 15 @Before("entryPoint()") 16 public void before(JoinPoint joinPoint) { 17 18 log.info("=====================开始执行前置通知=================="); 19 try { 20 String targetName = joinPoint.getTarget().getClass().getName(); 21 String methodName = joinPoint.getSignature().getName(); 22 Object[] arguments = joinPoint.getArgs(); 23 Class<?> targetClass = Class.forName(targetName); 24 Method[] methods = targetClass.getMethods(); 25 String operation = ""; 26 for (Method method : methods) { 27 if (method.getName().equals(methodName)) { 28 Class<?>[] clazzs = method.getParameterTypes(); 29 if (clazzs.length == arguments.length) { 30 operation = method.getAnnotation(ServiceLog.class).operation();// 操作人 31 break; 32 } 33 } 34 } 35 StringBuilder paramsBuf = new StringBuilder(); 36 for (Object arg : arguments) { 37 paramsBuf.append(arg); 38 paramsBuf.append("&"); 39 } 40 41 // *========控制台输出=========*// 42 log.info("[X用户]执行了[" + operation + "],类:" + targetName + ",方法名:" + methodName + ",参数:" 43 + paramsBuf.toString()); 44 log.info("=====================执行前置通知结束=================="); 45 } catch (Throwable e) { 46 log.info("around " + joinPoint + " with exception : " + e.getMessage()); 47 } 48 49 } 50 51 /** 52 * 环绕通知处理处理 53 * 54 * @param joinPoint 55 * @throws Throwable 56 */ 57 @Around("entryPoint()") 58 public Object around(ProceedingJoinPoint point) throws Throwable { 59 // 先执行业务,注意:业务这样写业务发生异常不会拦截日志。 60 Object result = point.proceed(); 61 try { 62 handleAround(point);// 处理日志 63 } catch (Exception e) { 64 log.error("日志记录异常", e); 65 } 66 return result; 67 } 68 69 /** 70 * around日志记录 71 * 72 * @param point 73 * @throws SecurityException 74 * @throws NoSuchMethodException 75 */ 76 public void handleAround(ProceedingJoinPoint point) throws Exception { 77 Signature sig = point.getSignature(); 78 MethodSignature msig = null; 79 if (!(sig instanceof MethodSignature)) { 80 throw new IllegalArgumentException("该注解只能用于方法"); 81 } 82 msig = (MethodSignature) sig; 83 Object target = point.getTarget(); 84 Method currentMethod = target.getClass().getMethod(msig.getName(), msig.getParameterTypes()); 85 // 方法名称 86 String methodName = currentMethod.getName(); 87 // 获取注解对象 88 ServiceLog aLog = currentMethod.getAnnotation(ServiceLog.class); 89 // 类名 90 String className = point.getTarget().getClass().getName(); 91 // 方法的参数 92 Object[] params = point.getArgs(); 93 94 StringBuilder paramsBuf = new StringBuilder(); 95 for (Object arg : params) { 96 paramsBuf.append(arg); 97 paramsBuf.append("&"); 98 } 99 // 处理log。。。。 100 log.info("[X用户]执行了[" + aLog.operation() + "],类:" + className + ",方法名:" + methodName + ",参数:" 101 + paramsBuf.toString()); 102 103 } 104 105 @AfterThrowing(pointcut = "entryPoint()", throwing = "e") 106 public void doAfterThrowing(JoinPoint joinPoint, Throwable e) { 107 // 通过request获取登陆用户信息 108 // HttpServletRequest request = ((ServletRequestAttributes) 109 // RequestContextHolder.getRequestAttributes()).getRequest(); 110 try { 111 String targetName = joinPoint.getTarget().getClass().getName(); 112 String className = joinPoint.getTarget().getClass().getName(); 113 String methodName = joinPoint.getSignature().getName(); 114 Object[] arguments = joinPoint.getArgs(); 115 Class<?> targetClass = Class.forName(targetName); 116 Method[] methods = targetClass.getMethods(); 117 String operation = ""; 118 for (Method method : methods) { 119 if (method.getName().equals(methodName)) { 120 Class<?>[] clazzs = method.getParameterTypes(); 121 if (clazzs.length == arguments.length) { 122 operation = method.getAnnotation(ServiceLog.class).operation(); 123 break; 124 } 125 } 126 } 127 128 StringBuilder paramsBuf = new StringBuilder(); 129 for (Object arg : arguments) { 130 paramsBuf.append(arg); 131 paramsBuf.append("&"); 132 } 133 134 log.info("异常方法:" + className + "." + methodName + "();参数:" + paramsBuf.toString() + ",处理了:" + operation); 135 log.info("异常信息:" + e.getMessage()); 136 } catch (Exception ex) { 137 log.error("异常信息:{}", ex.getMessage()); 138 } 139 }
1 public interface ILogService { 2 3 public boolean insert(Map<String, Object> params, String id); 4 5 public boolean update(String name, String id); 6 7 public boolean delete(String id); 8 9 public boolean doError(String id); 10 }
1 @Service 2 public class TestServiceImp implements ILogService { 3 4 @ServiceLog(operation = "新增用户信息测试操作。。。。。") 5 @Override 6 public boolean insert(Map<String, Object> params, String id) { 7 return false; 8 } 9 10 @ServiceLog(operation = "更新用户信息操作....") 11 @Override 12 public boolean update(String name, String id) { 13 return false; 14 } 15 16 @ServiceLog(operation = "删除操作。。。。") 17 @Override 18 public boolean delete(String id) { 19 return false; 20 } 21 22 @ServiceLog(operation = "异常操作测试", level = LogType.ERROR) 23 @Override 24 public boolean doError(String id) { 25 try { 26 @SuppressWarnings("unused") 27 int i = 1 / 0; 28 } catch (Exception e) { 29 throw new RuntimeException(e.getMessage()); 30 } 31 return false; 32 } 33 34 }
1 @RunWith(SpringRunner.class) 2 @SpringBootTest 3 public class DemoSpringbootAopLogApplicationTests { 4 5 @Autowired 6 ILogService logService; 7 8 @Test 9 public void contextLoads() { 10 Map<String, Object> params = new HashMap<>(); 11 params.put("key1", "v1"); 12 params.put("key2", "v2"); 13 14 logService.insert(params, "111"); 15 logService.update("king", "kang"); 16 logService.delete("111"); 17 logService.doError("111"); 18 } 19 20 }
至此,SpringCloud+AOP搭建成功!
项目完整代码见https://github.com/Adosker/hello
SpringCloud开发学习总结(六)—— 结合注解的AOP示例
标签:ott github 了解 user 方式 表达式 bar target 展示
原文地址:https://www.cnblogs.com/king-brook/p/9506024.html