前言
用Spring Boot的AOP来简化处理自定义注解,并将通过实现一个简单的方法执行判断节点是否开始的状态示列源码。
AOP概念
面向侧面的程序设计(aspect-oriented programming,AOP,又译作面向方面的程序设计、观点导向编程、剖面导向程序设计)是计算机科学中的一个术语,指一种程序设计范型。该范型以一种称为侧面(aspect,又译作方面)的语言构造为基础,侧面是一种新的模块化机制,用来描述分散在对象、类或函数中的横切关注点(crosscutting concern)。
Spring Boot的AOP环境准备
在pom.xml
中引入相应的依赖模块
<!-- AOP依赖模块 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
先建一个简单地建一个自定义注解类
一个简单地自定义注解类就生成了:
package com.common.annotation; import java.lang.annotation.Documented; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; /** * 自定义注解 * @Description 自定义注解@HAStatusCheck 拦截service层 HAStatusCheckService * @Author Justin zeng * @Date 2019-04-19 * @version 1.0 */ @Target({ElementType.PARAMETER, ElementType.METHOD}) @Retention(RetentionPolicy.RUNTIME) @Documented public @interface HAStatusCheck { //自定义的方法描述信息 String description() default ""; }

在建一个切点类实现你的要处理的事情
package com.common.aop; import java.lang.reflect.Executable; import java.lang.reflect.Method; import java.util.ArrayList; import javax.annotation.Resource; import org.aspectj.lang.JoinPoint; 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.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Component; import org.aspectj.lang.reflect.MethodSignature; import com.common.annotation.HAStatusCheck; import com.common.enums.ha.ServerPort; import com.common.enums.ha.ServerType; import com.common.ha.HAStatusCheckService; /** * 切点类 * @Description AOP拦截判断 HA节点是否开启状态方法Service层 * @Author Justin zeng * @Date 2019-04-19 * @version 1.0 */ @Aspect @Component public class HAStatusCheckAop { //注入Service @Resource private HAStatusCheckService hAStatusCheckService; private static final Logger LOG = LoggerFactory.getLogger(HAStatusCheckAop.class); //Service层切点 自定义的注解类路径 @Pointcut("@annotation(com.common.annotation.HAStatusCheck)") public void servicePointCut() {} Object proceed=null; @Around("servicePointCut()") public void logAroundService(ProceedingJoinPoint jointPoint) throws Throwable { LOG.info("------判断节点开始------"); ArrayList<String> list = new ArrayList<String>(); list.add("192.168.1.5"); if (!hAStatusCheckService.isActAsActiveNode(list, ServerPort.BILLING_HANDLER, ServerType.BILLING_HANDLER)) { LOG.info("------访问请求不合法!------"); return; //强制退出 //System.exit(0); }else { LOG.info("------判断注解开始------"); //获得当前访问的class Class<?> className = jointPoint.getTarget().getClass(); //获得访问的方法名 String methodName = jointPoint.getSignature().getName(); //得到方法的参数的类型 Class[] argClass = ((MethodSignature) jointPoint.getSignature()).getParameterTypes(); try { // 得到访问的方法对象 Method method = className.getDeclaredMethod(methodName, argClass); // 判断是否存在@HAStatusCheck注解 if (method.isAnnotationPresent(HAStatusCheck.class)) { HAStatusCheck annotation = method.getAnnotation(HAStatusCheck.class); annotation.description(); LOG.info("当前目标执行方法:"+methodName+"执行开始"); //执行当前目标中的方法 proceed = jointPoint.proceed(); LOG.info("当前目标class路径:"+className); LOG.info("当前目标执行方法名:" + methodName); LOG.info("当前目标执行方法的参数类型:" + argClass); LOG.info("当前目标方法执行的结果:" + proceed); LOG.info("当前目标执行方法:"+methodName+"执行结束"); } } catch (Exception e) { LOG.error("不存在@HAStatusCheck注解",e); } } } /** * 获取注解中对方法的描述信息 用于service层注解 * * @param joinPoint 切点 * @return 方法描述 * @throws Exception */ public static String getServiceMthodDescription(JoinPoint joinPoint) throws Exception { String targetName = joinPoint.getTarget().getClass().getName(); String methodName = joinPoint.getSignature().getName(); Object[] arguments = joinPoint.getArgs(); Class targetClass = Class.forName(targetName); Method[] methods = targetClass.getMethods(); String description = ""; for (Method method : methods) { if (method.getName().equals(methodName)) { Class[] clazzs = method.getParameterTypes(); if (clazzs.length == arguments.length) { description = method.getAnnotation(HAStatusCheck. class).description(); LOG.info("======"+description+"======"); break; } } } return description; } }

最后
在你需要的判断预处理的地方加上自定义的注解 @HAStatusCheck
package com.job; import com.annotation.HAStatusCheck; import lombok.extern.slf4j.Slf4j; @Slf4j public class TestJob { //这就是上面自定义的注解 @HAStatusCheck public void job() throws Exception { System.out.println("TestJob job run ...."); } }

这样自定义注解拦截service层开发就结束了,需要的可以试试吧,希望能帮到大家。