diff --git a/pom.xml b/pom.xml index 9aae7b0c..65e11015 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ com.codingapi.springboot springboot-parent - 1.5.2 + 1.5.3 https://github.com/codingapi/springboot-framewrok springboot-parent diff --git a/springboot-starter-data-fast/pom.xml b/springboot-starter-data-fast/pom.xml index 1e046b46..be63507d 100644 --- a/springboot-starter-data-fast/pom.xml +++ b/springboot-starter-data-fast/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 1.5.2 + 1.5.3 4.0.0 diff --git a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/DataFastConfiguration.java b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/DataFastConfiguration.java index c94d866c..c9717fdf 100644 --- a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/DataFastConfiguration.java +++ b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/DataFastConfiguration.java @@ -3,6 +3,7 @@ import com.codingapi.springboot.fast.executor.JpaExecutor; import com.codingapi.springboot.fast.mapping.MvcEndpointMapping; import com.codingapi.springboot.fast.registrar.MvcMappingRegistrar; +import org.springframework.aop.Advisor; import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; import org.springframework.context.annotation.Bean; @@ -11,6 +12,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping; import javax.persistence.EntityManager; +import java.util.List; @Configuration @ConditionalOnClass(WebMvcConfigurer.class) @@ -25,8 +27,10 @@ public MvcEndpointMapping mvcEndpointMapping(RequestMappingHandlerMapping handle @Bean(initMethod = "registerMvcMapping") @ConditionalOnMissingBean - public MvcMappingRegistrar mappingRegistrar(MvcEndpointMapping mvcEndpointMapping, JpaExecutor jpaExecutor) { - return new MvcMappingRegistrar(mvcEndpointMapping, jpaExecutor); + public MvcMappingRegistrar mappingRegistrar(MvcEndpointMapping mvcEndpointMapping, + JpaExecutor jpaExecutor, + List advisors) { + return new MvcMappingRegistrar(mvcEndpointMapping, jpaExecutor,advisors); } @Bean diff --git a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/executor/MvcMethodProxy.java b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/executor/MvcMethodInterceptor.java similarity index 73% rename from springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/executor/MvcMethodProxy.java rename to springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/executor/MvcMethodInterceptor.java index e8a26caa..fba74734 100644 --- a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/executor/MvcMethodProxy.java +++ b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/executor/MvcMethodInterceptor.java @@ -2,32 +2,38 @@ import com.codingapi.springboot.fast.annotation.FastMapping; import lombok.AllArgsConstructor; +import org.aopalliance.intercept.MethodInterceptor; +import org.aopalliance.intercept.MethodInvocation; -import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; @AllArgsConstructor -public class MvcMethodProxy implements InvocationHandler { +public class MvcMethodInterceptor implements MethodInterceptor { private final JpaExecutor jpaExecutor; @Override - public Object invoke(Object proxy, Method method, Object[] args) + public Object invoke(MethodInvocation invocation) throws Throwable { + Method method = invocation.getMethod(); + Object[] args = invocation.getArguments(); if (method.equals(Object.class.getMethod("equals", Object.class))) { return false; } + if (method.equals(Object.class.getMethod("hashCode"))) { return hashCode(); } + FastMapping fastMapping = method.getAnnotation(FastMapping.class); if (fastMapping != null) { Class returnType = method.getReturnType(); return jpaExecutor.execute(fastMapping.value(), fastMapping.countQuery(), args, returnType); } - // mvc mapping proxy can't execute return null. return null; } + + } \ No newline at end of file diff --git a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/registrar/MvcMappingRegistrar.java b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/registrar/MvcMappingRegistrar.java index 4757083a..e6a154b8 100644 --- a/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/registrar/MvcMappingRegistrar.java +++ b/springboot-starter-data-fast/src/main/java/com/codingapi/springboot/fast/registrar/MvcMappingRegistrar.java @@ -3,25 +3,42 @@ import com.codingapi.springboot.fast.annotation.FastMapping; import com.codingapi.springboot.fast.exception.FastMappingErrorException; import com.codingapi.springboot.fast.executor.JpaExecutor; -import com.codingapi.springboot.fast.executor.MvcMethodProxy; +import com.codingapi.springboot.fast.executor.MvcMethodInterceptor; import com.codingapi.springboot.fast.mapping.MvcEndpointMapping; -import lombok.AllArgsConstructor; import lombok.SneakyThrows; import lombok.extern.slf4j.Slf4j; +import org.springframework.aop.Advisor; +import org.springframework.aop.framework.AdvisedSupport; +import org.springframework.aop.framework.AopProxy; +import org.springframework.aop.framework.AopProxyFactory; +import org.springframework.aop.framework.DefaultAopProxyFactory; import org.springframework.data.domain.Pageable; import org.springframework.util.StringUtils; import java.lang.reflect.Method; -import java.lang.reflect.Proxy; import java.util.HashSet; +import java.util.List; import java.util.Set; @Slf4j -@AllArgsConstructor public class MvcMappingRegistrar { protected final static Set> classSet = new HashSet<>(); private final MvcEndpointMapping mvcEndpointMapping; - private final JpaExecutor jpaExecutor; + + private final AopProxyFactory proxyFactory; + + private final List advisors; + + private final MvcMethodInterceptor interceptor; + + public MvcMappingRegistrar(MvcEndpointMapping mvcEndpointMapping, + JpaExecutor jpaExecutor, + List advisors) { + this.mvcEndpointMapping = mvcEndpointMapping; + this.advisors = advisors; + this.interceptor = new MvcMethodInterceptor(jpaExecutor); + this.proxyFactory = new DefaultAopProxyFactory(); + } @SneakyThrows public void registerMvcMapping() { @@ -30,36 +47,47 @@ public void registerMvcMapping() { for (Method method : methods) { FastMapping fastMapping = method.getAnnotation(FastMapping.class); if (verify(fastMapping, method)) { - MvcMethodProxy handler = new MvcMethodProxy(jpaExecutor); - Object methodProxy = Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, handler); - mvcEndpointMapping.addMapping(fastMapping.mapping(), fastMapping.method(), methodProxy, method); + AdvisedSupport advisedSupport = createAdvisedSupport(clazz); + AopProxy proxy = proxyFactory.createAopProxy(advisedSupport); + mvcEndpointMapping.addMapping(fastMapping.mapping(), fastMapping.method(), + proxy.getProxy(), method); } } } } + private AdvisedSupport createAdvisedSupport(Class clazz) { + AdvisedSupport advisedSupport = new AdvisedSupport(clazz); + advisedSupport.setTarget(interceptor); + advisedSupport.addAdvisors(advisors); + advisedSupport.addAdvice(interceptor); + return advisedSupport; + } + private boolean verify(FastMapping fastMapping, Method method) throws FastMappingErrorException { if (fastMapping == null) { return false; } if (!StringUtils.hasText(fastMapping.mapping())) { - throw new FastMappingErrorException(String.format("fast method %s missing mapping .", method.getName())); + throw new FastMappingErrorException(String.format("fast method %s missing mapping .", + method.getName())); } if (!StringUtils.hasText(fastMapping.value())) { - throw new FastMappingErrorException(String.format("fast mapping %s missing value .", fastMapping.mapping())); + throw new FastMappingErrorException(String.format("fast mapping %s missing value .", + fastMapping.mapping())); } Class[] parameterTypes = method.getParameterTypes(); for (Class parameter : parameterTypes) { if (Pageable.class.isAssignableFrom(parameter)) { if (!StringUtils.hasText(fastMapping.countQuery())) { - throw new FastMappingErrorException(String.format("fast mapping %s missing countQuery .", fastMapping.mapping())); + throw new FastMappingErrorException(String.format("fast mapping %s missing countQuery .", + fastMapping.mapping())); } } } - return true; } diff --git a/springboot-starter-id-generator/pom.xml b/springboot-starter-id-generator/pom.xml index f5230f4b..745d1eac 100644 --- a/springboot-starter-id-generator/pom.xml +++ b/springboot-starter-id-generator/pom.xml @@ -5,7 +5,7 @@ springboot-parent com.codingapi.springboot - 1.5.2 + 1.5.3 4.0.0 diff --git a/springboot-starter-security-jwt/pom.xml b/springboot-starter-security-jwt/pom.xml index 57182894..509ec2a1 100644 --- a/springboot-starter-security-jwt/pom.xml +++ b/springboot-starter-security-jwt/pom.xml @@ -6,7 +6,7 @@ springboot-parent com.codingapi.springboot - 1.5.2 + 1.5.3 springboot-starter-security-jwt diff --git a/springboot-starter/pom.xml b/springboot-starter/pom.xml index 08165ab2..c856dbe1 100644 --- a/springboot-starter/pom.xml +++ b/springboot-starter/pom.xml @@ -5,7 +5,7 @@ com.codingapi.springboot springboot-parent - 1.5.2 + 1.5.3 springboot-starter