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 static org .hamcrest .CoreMatchers .is ;
20
- import static org .junit .Assert .assertEquals ;
21
- import static org .junit .Assert .assertThat ;
22
- import static org .junit .Assert .assertTrue ;
19
+ import java .lang .annotation .Retention ;
20
+ import java .lang .annotation .RetentionPolicy ;
23
21
22
+ import example .scannable .FooService ;
23
+ import example .scannable .ServiceInvocationCounter ;
24
+ import org .aspectj .lang .annotation .Aspect ;
25
+ import org .aspectj .lang .annotation .Before ;
24
26
import org .junit .Test ;
27
+
25
28
import org .springframework .aop .support .AopUtils ;
26
29
import org .springframework .context .ApplicationContext ;
30
+ import org .springframework .context .ConfigurableApplicationContext ;
27
31
28
- import example . scannable . FooService ;
29
- import example . scannable . ServiceInvocationCounter ;
32
+ import static org . hamcrest . CoreMatchers .* ;
33
+ import static org . junit . Assert .* ;
30
34
31
35
/**
32
36
* @author Juergen Hoeller
33
37
* @author Chris Beams
34
38
*/
35
39
public class EnableAspectJAutoProxyTests {
36
40
37
- @ Configuration
38
- @ ComponentScan ("example.scannable" )
39
- @ EnableAspectJAutoProxy
40
- static class Config_WithJDKProxy {
41
- }
42
-
43
- @ Configuration
44
- @ ComponentScan ("example.scannable" )
45
- @ EnableAspectJAutoProxy (proxyTargetClass =true )
46
- static class Config_WithCGLIBProxy {
47
- }
48
-
49
41
@ Test
50
- public void withJDKProxy () throws Exception {
51
- ApplicationContext ctx =
52
- new AnnotationConfigApplicationContext (Config_WithJDKProxy .class );
42
+ public void withJdkProxy () {
43
+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithJdkProxy .class );
53
44
54
45
aspectIsApplied (ctx );
55
46
assertThat (AopUtils .isJdkDynamicProxy (ctx .getBean (FooService .class )), is (true ));
56
47
}
57
48
58
49
@ Test
59
- public void withCGLIBProxy () throws Exception {
60
- ApplicationContext ctx =
61
- new AnnotationConfigApplicationContext (Config_WithCGLIBProxy .class );
50
+ public void withCglibProxy () {
51
+ ApplicationContext ctx = new AnnotationConfigApplicationContext (ConfigWithCglibProxy .class );
62
52
63
53
aspectIsApplied (ctx );
64
54
assertThat (AopUtils .isCglibProxy (ctx .getBean (FooService .class )), is (true ));
65
55
}
66
56
67
-
68
- private void aspectIsApplied (ApplicationContext ctx ) throws Exception {
57
+ private void aspectIsApplied (ApplicationContext ctx ) {
69
58
FooService fooService = ctx .getBean (FooService .class );
70
59
ServiceInvocationCounter counter = ctx .getBean (ServiceInvocationCounter .class );
71
60
@@ -81,4 +70,73 @@ private void aspectIsApplied(ApplicationContext ctx) throws Exception {
81
70
fooService .foo (1 );
82
71
assertEquals (3 , counter .getCount ());
83
72
}
84
- }
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