Parcourir la source

代码检测,检测service层的方法作者和参数必填项

herryhaixiao il y a 6 ans
Parent
commit
4dafc1e7ee

+ 45 - 30
elab-spring/src/main/java/com/elab/spring/intercept/CheckAnnotationProcess.java

@@ -1,7 +1,7 @@
 package com.elab.spring.intercept;
 
 import com.elab.core.aop.annotations.ExceptionHandle;
-import com.elab.core.exception.CheckException;
+import com.elab.core.utils.ObjectUtils;
 import com.elab.core.utils.StringUtils;
 import io.swagger.annotations.ApiOperation;
 import org.slf4j.Logger;
@@ -11,7 +11,7 @@ import org.springframework.beans.BeansException;
 import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
 import org.springframework.beans.factory.config.BeanPostProcessor;
 import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
-import org.springframework.stereotype.Service;
+import org.springframework.core.env.Environment;
 import org.springframework.transaction.annotation.Transactional;
 
 import java.lang.reflect.Method;
@@ -21,12 +21,21 @@ import java.lang.reflect.Method;
  * @Author : liukx
  * @time : 2019/3/6 - 11:44
  */
-@Service
 public class CheckAnnotationProcess implements BeanPostProcessor, BeanFactoryPostProcessor {
 
-    private Logger logger = LoggerFactory.getLogger(com.elab.spring.process.CheckAnnotationProcess.class);
+    private Logger logger = LoggerFactory.getLogger(com.elab.spring.intercept.CheckAnnotationProcess.class);
     private ConfigurableListableBeanFactory beanFactory;
+
+    private Environment environment;
     private boolean isException = false;
+    /*排除不包含的包名*/
+    private String excludePackage;
+
+    public CheckAnnotationProcess(Environment environment) {
+        this.environment = environment;
+        this.isException = ObjectUtils.isNotEmpty(environment.getProperty("elab.isException")) ? Boolean.parseBoolean(environment.getProperty("elab.isException")): false;
+        this.excludePackage = environment.getProperty("elab.checkPackage");
+    }
 
     public void setException(boolean exception) {
         isException = exception;
@@ -44,35 +53,41 @@ public class CheckAnnotationProcess implements BeanPostProcessor, BeanFactoryPos
 
     @Override
     public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
-        String exclude = "com.elab.marketing.auth.dao";
-        String proxyName = "com.sun.proxy";
-        if (AopUtils.isAopProxy(bean)) {
-            Class<?> targetClass = AopUtils.getTargetClass(bean);
-            String name = targetClass.getName();
-            if (!name.startsWith(exclude) && !name.startsWith(proxyName)) {
-                Method[] declaredMethods = targetClass.getDeclaredMethods();
-                for (int i = 0; i < declaredMethods.length; i++) {
-                    Method declaredMethod = declaredMethods[i];
-                    String author = "";
-                    Transactional annotation = declaredMethod.getAnnotation(Transactional.class);
-                    ApiOperation apiOperation = declaredMethod.getAnnotation(ApiOperation.class);
-                    if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.nickname())) {
-                        author = apiOperation.nickname();
-                    }
-                    if (annotation != null || apiOperation != null) {
-                        ExceptionHandle exceptionHandle = declaredMethod.getAnnotation(ExceptionHandle.class);
-                        if (exceptionHandle != null) {
-                            if (StringUtils.isEmpty(exceptionHandle.username())) {
-                                String message = "请在" + name + " \t " + declaredMethod.getName() + " --> " + author + " 方法头的@ExceptionHandle 加上username属性";
-                                notifyWarning(message);
+        String[] checkPackageList = excludePackage.split(",");
+        if (checkPackageList.length > 0) {
+            String exclude = "com.elab.marketing.auth.dao";
+            String proxyName = "com.sun.proxy";
+            if (AopUtils.isAopProxy(bean)) {
+                Class<?> targetClass = AopUtils.getTargetClass(bean);
+                String name = targetClass.getName();
+
+
+                for(String singlePackage : checkPackageList) {
+                    if (!name.startsWith(singlePackage) && !name.startsWith(proxyName)) {
+                        Method[] declaredMethods = targetClass.getDeclaredMethods();
+                        for (int i = 0; i < declaredMethods.length; i++) {
+                            Method declaredMethod = declaredMethods[i];
+                            String author = "";
+                            Transactional annotation = declaredMethod.getAnnotation(Transactional.class);
+                            ApiOperation apiOperation = declaredMethod.getAnnotation(ApiOperation.class);
+                            if (apiOperation != null && StringUtils.isNotEmpty(apiOperation.nickname())) {
+                                author = apiOperation.nickname();
+                            }
+                            if (annotation != null || apiOperation != null) {
+                                ExceptionHandle exceptionHandle = declaredMethod.getAnnotation(ExceptionHandle.class);
+                                if (exceptionHandle != null) {
+                                    if (StringUtils.isEmpty(exceptionHandle.username())) {
+                                        String message = "请在" + name + " \t " + declaredMethod.getName() + " --> " + author + " 方法头的@ExceptionHandle 加上username属性";
+                                        notifyWarning(message);
+                                    }
+                                } else {
+                                    String message = "请在" + name + " \t " + declaredMethod.getName() + " --> " + author + " 方法头加上 @ExceptionHandle(username = '')";
+                                    notifyWarning(message);
+                                }
                             }
-                        } else {
-                            String message = "请在" + name + " \t " + declaredMethod.getName() + " --> " + author + " 方法头加上 @ExceptionHandle(username = '')";
-                            notifyWarning(message);
                         }
                     }
                 }
-
             }
         }
         return bean;
@@ -80,7 +95,7 @@ public class CheckAnnotationProcess implements BeanPostProcessor, BeanFactoryPos
 
     private void notifyWarning(String message) {
         if (isException) {
-            throw new CheckException(message);
+            throw new RuntimeException(message);
         } else {
             logger.error(message);
         }