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 ;
88import lombok .AllArgsConstructor ;
99import lombok .SneakyThrows ;
1010import lombok .extern .slf4j .Slf4j ;
11+ import org .springframework .aop .Advisor ;
12+ import org .springframework .aop .framework .AdvisedSupport ;
13+ import org .springframework .aop .framework .AopProxy ;
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
@@ -23,43 +27,59 @@ public class MvcMappingRegistrar {
2327 private final MvcEndpointMapping mvcEndpointMapping ;
2428 private final JpaExecutor jpaExecutor ;
2529
30+ private final DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory ();
31+
32+ private final List <Advisor > advisors ;
33+
2634 @ SneakyThrows
2735 public void registerMvcMapping () {
2836 for (Class <?> clazz : classSet ) {
2937 Method [] methods = clazz .getDeclaredMethods ();
3038 for (Method method : methods ) {
3139 FastMapping fastMapping = method .getAnnotation (FastMapping .class );
3240 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 );
41+ AdvisedSupport advisedSupport = createAdvisedSupport (clazz );
42+ AopProxy proxy = proxyFactory .createAopProxy (advisedSupport );
43+ mvcEndpointMapping .addMapping (fastMapping .mapping (), fastMapping .method (),
44+ proxy .getProxy (), method );
3645 }
3746 }
3847 }
3948 }
4049
50+ private AdvisedSupport createAdvisedSupport (Class <?> clazz ){
51+ AdvisedSupport advisedSupport = new AdvisedSupport (clazz );
52+ MvcMethodInterceptor interceptor = new MvcMethodInterceptor (jpaExecutor );
53+ advisedSupport .setTarget (interceptor );
54+ advisedSupport .addAdvisors (advisors );
55+ advisedSupport .addAdvice (interceptor );
56+ return advisedSupport ;
57+ }
58+
4159 private boolean verify (FastMapping fastMapping , Method method ) throws FastMappingErrorException {
4260 if (fastMapping == null ) {
4361 return false ;
4462 }
4563
4664 if (!StringUtils .hasText (fastMapping .mapping ())) {
47- throw new FastMappingErrorException (String .format ("fast method %s missing mapping ." , method .getName ()));
65+ throw new FastMappingErrorException (String .format ("fast method %s missing mapping ." ,
66+ method .getName ()));
4867 }
4968
5069 if (!StringUtils .hasText (fastMapping .value ())) {
51- throw new FastMappingErrorException (String .format ("fast mapping %s missing value ." , fastMapping .mapping ()));
70+ throw new FastMappingErrorException (String .format ("fast mapping %s missing value ." ,
71+ fastMapping .mapping ()));
5272 }
5373
5474 Class <?>[] parameterTypes = method .getParameterTypes ();
5575 for (Class <?> parameter : parameterTypes ) {
5676 if (Pageable .class .isAssignableFrom (parameter )) {
5777 if (!StringUtils .hasText (fastMapping .countQuery ())) {
58- throw new FastMappingErrorException (String .format ("fast mapping %s missing countQuery ." , fastMapping .mapping ()));
78+ throw new FastMappingErrorException (String .format ("fast mapping %s missing countQuery ." ,
79+ fastMapping .mapping ()));
5980 }
6081 }
6182 }
62-
6383 return true ;
6484 }
6585
0 commit comments