1
1
/*
2
- * Copyright 2002-2011 the original author or authors.
2
+ * Copyright 2002-2015 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
16
16
17
17
package org .springframework .context .annotation ;
18
18
19
+ import java .lang .annotation .Retention ;
20
+ import java .lang .annotation .RetentionPolicy ;
21
+
19
22
import example .scannable .FooService ;
20
23
import example .scannable .ServiceInvocationCounter ;
24
+ import org .aspectj .lang .annotation .Aspect ;
25
+ import org .aspectj .lang .annotation .Before ;
21
26
import org .junit .Test ;
22
27
23
28
import org .springframework .aop .support .AopUtils ;
24
29
import org .springframework .context .ApplicationContext ;
30
+ import org .springframework .context .ConfigurableApplicationContext ;
25
31
26
32
import static org .hamcrest .CoreMatchers .*;
27
33
import static org .junit .Assert .*;
32
38
*/
33
39
public class EnableAspectJAutoProxyTests {
34
40
35
- @ Configuration
36
- @ ComponentScan ("example.scannable" )
37
- @ EnableAspectJAutoProxy
38
- static class Config_WithJDKProxy {
39
- }
40
-
41
- @ Configuration
42
- @ ComponentScan ("example.scannable" )
43
- @ EnableAspectJAutoProxy (proxyTargetClass =true )
44
- static class Config_WithCGLIBProxy {
45
- }
46
-
47
41
@ Test
48
- public void withJDKProxy () throws Exception {
49
- ApplicationContext ctx =
50
- new AnnotationConfigApplicationContext (Config_WithJDKProxy .class );
42
+ public void withJdkProxy () {
43
+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithJdkProxy .class );
51
44
52
45
aspectIsApplied (ctx );
53
46
assertThat (AopUtils .isJdkDynamicProxy (ctx .getBean (FooService .class )), is (true ));
54
47
}
55
48
56
49
@ Test
57
- public void withCGLIBProxy () throws Exception {
58
- ApplicationContext ctx =
59
- new AnnotationConfigApplicationContext (Config_WithCGLIBProxy .class );
50
+ public void withCglibProxy () {
51
+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithCglibProxy .class );
60
52
61
53
aspectIsApplied (ctx );
62
54
assertThat (AopUtils .isCglibProxy (ctx .getBean (FooService .class )), is (true ));
63
55
}
64
56
65
-
66
- private void aspectIsApplied (ApplicationContext ctx ) throws Exception {
57
+ private void aspectIsApplied (ApplicationContext ctx ) {
67
58
FooService fooService = ctx .getBean (FooService .class );
68
59
ServiceInvocationCounter counter = ctx .getBean (ServiceInvocationCounter .class );
69
60
@@ -79,4 +70,73 @@ private void aspectIsApplied(ApplicationContext ctx) throws Exception {
79
70
fooService .foo (1 );
80
71
assertEquals (3 , counter .getCount ());
81
72
}
82
- }
73
+
74
+ @ Test
75
+ public void withAnnotationOnArgumentAndJdkProxy () {
76
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (
77
+ ConfigWithJdkProxy .class , SampleService .class , LoggingAspect .class );
78
+
79
+ SampleService sampleService = ctx .getBean (SampleService .class );
80
+ sampleService .execute (new SampleDto ());
81
+ sampleService .execute (new SampleInputBean ());
82
+ sampleService .execute ((SampleDto ) null );
83
+ sampleService .execute ((SampleInputBean ) null );
84
+ }
85
+
86
+ @ Test
87
+ public void withAnnotationOnArgumentAndCglibProxy () {
88
+ ConfigurableApplicationContext ctx = new AnnotationConfigApplicationContext (
89
+ ConfigWithCglibProxy .class , SampleService .class , LoggingAspect .class );
90
+
91
+ SampleService sampleService = ctx .getBean (SampleService .class );
92
+ sampleService .execute (new SampleDto ());
93
+ sampleService .execute (new SampleInputBean ());
94
+ sampleService .execute ((SampleDto ) null );
95
+ sampleService .execute ((SampleInputBean ) null );
96
+ }
97
+
98
+
99
+ @ Configuration
100
+ @ ComponentScan ("example.scannable" )
101
+ @ EnableAspectJAutoProxy
102
+ static class ConfigWithJdkProxy {
103
+ }
104
+
105
+ @ Configuration
106
+ @ ComponentScan ("example.scannable" )
107
+ @ EnableAspectJAutoProxy (proxyTargetClass = true )
108
+ static class ConfigWithCglibProxy {
109
+ }
110
+
111
+
112
+ @ Retention (RetentionPolicy .RUNTIME )
113
+ public @interface Loggable {
114
+ }
115
+
116
+ @ Loggable
117
+ public static class SampleDto {
118
+ }
119
+
120
+ public static class SampleInputBean {
121
+ }
122
+
123
+ public static class SampleService {
124
+
125
+ // Not matched method on {@link LoggingAspect}.
126
+ public void execute (SampleInputBean inputBean ) {
127
+ }
128
+
129
+ // Matched method on {@link LoggingAspect}
130
+ public void execute (SampleDto dto ) {
131
+ }
132
+ }
133
+
134
+ @ Aspect
135
+ public static class LoggingAspect {
136
+
137
+ @ Before ("@args(org.springframework.context.annotation.EnableAspectJAutoProxyTests.Loggable))" )
138
+ public void loggingBeginByAtArgs () {
139
+ }
140
+ }
141
+
142
+ }
0 commit comments