3
3
import com .codingapi .springboot .fast .annotation .FastMapping ;
4
4
import com .codingapi .springboot .fast .exception .FastMappingErrorException ;
5
5
import com .codingapi .springboot .fast .executor .JpaExecutor ;
6
- import com .codingapi .springboot .fast .executor .MvcMethodProxy ;
6
+ import com .codingapi .springboot .fast .executor .MvcMethodInterceptor ;
7
7
import com .codingapi .springboot .fast .mapping .MvcEndpointMapping ;
8
8
import lombok .AllArgsConstructor ;
9
9
import lombok .SneakyThrows ;
10
10
import 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 ;
11
15
import org .springframework .data .domain .Pageable ;
12
16
import org .springframework .util .StringUtils ;
13
17
14
18
import java .lang .reflect .Method ;
15
- import java .lang .reflect .Proxy ;
16
19
import java .util .HashSet ;
20
+ import java .util .List ;
17
21
import java .util .Set ;
18
22
19
23
@ Slf4j
@@ -23,43 +27,59 @@ public class MvcMappingRegistrar {
23
27
private final MvcEndpointMapping mvcEndpointMapping ;
24
28
private final JpaExecutor jpaExecutor ;
25
29
30
+ private final DefaultAopProxyFactory proxyFactory = new DefaultAopProxyFactory ();
31
+
32
+ private final List <Advisor > advisors ;
33
+
26
34
@ SneakyThrows
27
35
public void registerMvcMapping () {
28
36
for (Class <?> clazz : classSet ) {
29
37
Method [] methods = clazz .getDeclaredMethods ();
30
38
for (Method method : methods ) {
31
39
FastMapping fastMapping = method .getAnnotation (FastMapping .class );
32
40
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 );
36
45
}
37
46
}
38
47
}
39
48
}
40
49
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
+
41
59
private boolean verify (FastMapping fastMapping , Method method ) throws FastMappingErrorException {
42
60
if (fastMapping == null ) {
43
61
return false ;
44
62
}
45
63
46
64
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 ()));
48
67
}
49
68
50
69
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 ()));
52
72
}
53
73
54
74
Class <?>[] parameterTypes = method .getParameterTypes ();
55
75
for (Class <?> parameter : parameterTypes ) {
56
76
if (Pageable .class .isAssignableFrom (parameter )) {
57
77
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 ()));
59
80
}
60
81
}
61
82
}
62
-
63
83
return true ;
64
84
}
65
85
0 commit comments