Parcourir la source

新增aop增强模块

zhouhao il y a 8 ans
Parent
commit
ed9a3f34aa

+ 3 - 0
hsweb-boost/hsweb-boost-aop/README.md

@@ -0,0 +1,3 @@
+# AOP增强模块
+提供aop常用操作需要的公共类
+ 

+ 57 - 0
hsweb-boost/hsweb-boost-aop/pom.xml

@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~  Copyright 2016 http://www.hswebframework.org
+  ~
+  ~  Licensed under the Apache License, Version 2.0 (the "License");
+  ~  you may not use this file except in compliance with the License.
+  ~  You may obtain a copy of the License at
+  ~
+  ~        http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~  Unless required by applicable law or agreed to in writing, software
+  ~  distributed under the License is distributed on an "AS IS" BASIS,
+  ~  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  ~  See the License for the specific language governing permissions and
+  ~  limitations under the License.
+  ~
+  ~
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0"
+         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+    <parent>
+        <artifactId>hsweb-boost</artifactId>
+        <groupId>org.hswebframework.web</groupId>
+        <version>3.0-SNAPSHOT</version>
+    </parent>
+    <modelVersion>4.0.0</modelVersion>
+
+    <artifactId>hsweb-boost-aop</artifactId>
+
+    <dependencies>
+        <dependency>
+            <groupId>org.hswebframework.web</groupId>
+            <artifactId>hsweb-commons-utils</artifactId>
+            <version>${project.version}</version>
+        </dependency>
+
+        <dependency>
+            <groupId>org.springframework.boot</groupId>
+            <artifactId>spring-boot-starter-web</artifactId>
+            <optional>true</optional>
+        </dependency>
+        <dependency>
+            <groupId>org.aspectj</groupId>
+            <artifactId>aspectjweaver</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.slf4j</groupId>
+            <artifactId>slf4j-api</artifactId>
+        </dependency>
+        <dependency>
+            <groupId>org.hswebframework</groupId>
+            <artifactId>hsweb-utils</artifactId>
+        </dependency>
+    </dependencies>
+</project>

+ 26 - 2
hsweb-boost/hsweb-boost-aop/src/main/java/org/hswebframework/web/boost/aop/context/MethodInterceptorHolder.java

@@ -18,11 +18,16 @@
 
 package org.hswebframework.web.boost.aop.context;
 
+import org.aopalliance.intercept.MethodInvocation;
 import org.hswebframework.web.AopUtils;
 import org.hswebframework.web.ThreadLocalUtils;
+import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
+import org.springframework.core.ParameterNameDiscoverer;
+import org.springframework.util.DigestUtils;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
+import java.util.LinkedHashMap;
 import java.util.Map;
 import java.util.Objects;
 import java.util.Optional;
@@ -31,6 +36,7 @@ import java.util.Optional;
  * @author zhouhao
  */
 public class MethodInterceptorHolder {
+    public static final ParameterNameDiscoverer nameDiscoverer = new LocalVariableTableParameterNameDiscoverer();
 
     public static MethodInterceptorHolder current() {
         return ThreadLocalUtils.get(MethodInterceptorHolder.class.getName());
@@ -44,6 +50,19 @@ public class MethodInterceptorHolder {
         return ThreadLocalUtils.put(MethodInterceptorHolder.class.getName(), holder);
     }
 
+    public static MethodInterceptorHolder create(MethodInvocation invocation) {
+        String id = DigestUtils.md5DigestAsHex(String.valueOf(invocation.getMethod().hashCode()).getBytes());
+        String[] argNames = nameDiscoverer.getParameterNames(invocation.getMethod());
+        Object[] args = invocation.getArguments();
+        Map<String, Object> argMap = new LinkedHashMap<>();
+        for (int i = 0, len = args.length; i < len; i++) {
+            argMap.put(argNames[i] == null ? "arg" + i : argNames[i], args[i]);
+        }
+        return new MethodInterceptorHolder(id,
+                invocation.getMethod(),
+                invocation.getThis(), argMap);
+    }
+
     private String id;
 
     private Method method;
@@ -52,8 +71,9 @@ public class MethodInterceptorHolder {
 
     private Map<String, Object> args;
 
-    public void set() {
+    public MethodInterceptorHolder set() {
         MethodInterceptorHolder.setCurrent(this);
+        return this;
     }
 
     public MethodInterceptorHolder(String id, Method method, Object target, Map<String, Object> args) {
@@ -84,6 +104,10 @@ public class MethodInterceptorHolder {
         return args;
     }
 
+    public <T extends Annotation> T findAnnotation(Class<T> annClass) {
+        return AopUtils.findAnnotation(target.getClass(), method, annClass);
+    }
+
     public MethodInterceptorParamContext createParamContext() {
         return new MethodInterceptorParamContext() {
             @Override
@@ -104,7 +128,7 @@ public class MethodInterceptorHolder {
 
             @Override
             public <T extends Annotation> T getAnnotation(Class<T> annClass) {
-                return AopUtils.findAnnotation(target.getClass(), method, annClass);
+                return findAnnotation(annClass);
             }
 
             @Override