33import com .codingapi .springboot .fast .annotation .FastMapping ;
44import com .codingapi .springboot .fast .exception .FastMappingErrorException ;
55import com .codingapi .springboot .fast .executor .JpaExecutor ;
6- import com .codingapi .springboot .fast .executor .MvcMethodProxy ;
6+ import com .codingapi .springboot .fast .executor .MvcMethodInterceptor ;
77import com .codingapi .springboot .fast .mapping .MvcEndpointMapping ;
8- import lombok .AllArgsConstructor ;
98import lombok .SneakyThrows ;
109import lombok .extern .slf4j .Slf4j ;
10+ import org .springframework .aop .Advisor ;
11+ import org .springframework .aop .framework .AdvisedSupport ;
12+ import org .springframework .aop .framework .AopProxy ;
13+ import org .springframework .aop .framework .AopProxyFactory ;
14+ import org .springframework .aop .framework .DefaultAopProxyFactory ;
1115import org .springframework .data .domain .Pageable ;
1216import org .springframework .util .StringUtils ;
1317
1418import java .lang .reflect .Method ;
15- import java .lang .reflect .Proxy ;
1619import java .util .HashSet ;
20+ import java .util .List ;
1721import java .util .Set ;
1822
1923@ Slf4j
20- @ AllArgsConstructor
2124public class MvcMappingRegistrar {
2225 protected final static Set <Class <?>> classSet = new HashSet <>();
2326 private final MvcEndpointMapping mvcEndpointMapping ;
24- private final JpaExecutor jpaExecutor ;
27+
28+ private final AopProxyFactory proxyFactory ;
29+
30+ private final List <Advisor > advisors ;
31+
32+ private final MvcMethodInterceptor interceptor ;
33+
34+ public MvcMappingRegistrar (MvcEndpointMapping mvcEndpointMapping ,
35+ JpaExecutor jpaExecutor ,
36+ List <Advisor > advisors ) {
37+ this .mvcEndpointMapping = mvcEndpointMapping ;
38+ this .advisors = advisors ;
39+ this .interceptor = new MvcMethodInterceptor (jpaExecutor );
40+ this .proxyFactory = new DefaultAopProxyFactory ();
41+ }
2542
2643 @ SneakyThrows
2744 public void registerMvcMapping () {
@@ -30,36 +47,47 @@ public void registerMvcMapping() {
3047 for (Method method : methods ) {
3148 FastMapping fastMapping = method .getAnnotation (FastMapping .class );
3249 if (verify (fastMapping , method )) {
33- MvcMethodProxy handler = new MvcMethodProxy (jpaExecutor );
34- Object methodProxy = Proxy .newProxyInstance (clazz .getClassLoader (), new Class []{clazz }, handler );
35- mvcEndpointMapping .addMapping (fastMapping .mapping (), fastMapping .method (), methodProxy , method );
50+ AdvisedSupport advisedSupport = createAdvisedSupport (clazz );
51+ AopProxy proxy = proxyFactory .createAopProxy (advisedSupport );
52+ mvcEndpointMapping .addMapping (fastMapping .mapping (), fastMapping .method (),
53+ proxy .getProxy (), method );
3654 }
3755 }
3856 }
3957 }
4058
59+ private AdvisedSupport createAdvisedSupport (Class <?> clazz ) {
60+ AdvisedSupport advisedSupport = new AdvisedSupport (clazz );
61+ advisedSupport .setTarget (interceptor );
62+ advisedSupport .addAdvisors (advisors );
63+ advisedSupport .addAdvice (interceptor );
64+ return advisedSupport ;
65+ }
66+
4167 private boolean verify (FastMapping fastMapping , Method method ) throws FastMappingErrorException {
4268 if (fastMapping == null ) {
4369 return false ;
4470 }
4571
4672 if (!StringUtils .hasText (fastMapping .mapping ())) {
47- throw new FastMappingErrorException (String .format ("fast method %s missing mapping ." , method .getName ()));
73+ throw new FastMappingErrorException (String .format ("fast method %s missing mapping ." ,
74+ method .getName ()));
4875 }
4976
5077 if (!StringUtils .hasText (fastMapping .value ())) {
51- throw new FastMappingErrorException (String .format ("fast mapping %s missing value ." , fastMapping .mapping ()));
78+ throw new FastMappingErrorException (String .format ("fast mapping %s missing value ." ,
79+ fastMapping .mapping ()));
5280 }
5381
5482 Class <?>[] parameterTypes = method .getParameterTypes ();
5583 for (Class <?> parameter : parameterTypes ) {
5684 if (Pageable .class .isAssignableFrom (parameter )) {
5785 if (!StringUtils .hasText (fastMapping .countQuery ())) {
58- throw new FastMappingErrorException (String .format ("fast mapping %s missing countQuery ." , fastMapping .mapping ()));
86+ throw new FastMappingErrorException (String .format ("fast mapping %s missing countQuery ." ,
87+ fastMapping .mapping ()));
5988 }
6089 }
6190 }
62-
6391 return true ;
6492 }
6593
0 commit comments