1
1
/*
2
- * Copyright 2002-2013 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 .configuration ;
18
18
19
+ import org .aspectj .lang .JoinPoint ;
20
+ import org .aspectj .lang .annotation .After ;
19
21
import org .aspectj .lang .annotation .Aspect ;
20
22
import org .aspectj .lang .annotation .Before ;
21
23
import org .junit .Test ;
22
24
23
25
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
24
26
import org .springframework .beans .factory .support .RootBeanDefinition ;
25
27
import org .springframework .beans .factory .xml .XmlBeanDefinitionReader ;
28
+ import org .springframework .context .ApplicationContext ;
29
+ import org .springframework .context .annotation .AnnotationConfigApplicationContext ;
26
30
import org .springframework .context .annotation .Bean ;
27
31
import org .springframework .context .annotation .Configuration ;
28
32
import org .springframework .context .annotation .ConfigurationClassPostProcessor ;
33
+ import org .springframework .context .annotation .EnableAspectJAutoProxy ;
29
34
import org .springframework .context .support .GenericApplicationContext ;
30
35
import org .springframework .core .io .ClassPathResource ;
31
36
import org .springframework .tests .sample .beans .TestBean ;
44
49
* processing of the Aspect annotation.
45
50
*
46
51
* @author Chris Beams
52
+ * @author Juergen Hoeller
47
53
*/
48
54
public class ConfigurationClassAspectIntegrationTests {
55
+
56
+ @ Test
57
+ public void aspectAnnotatedConfiguration () {
58
+ assertAdviceWasApplied (AspectConfig .class );
59
+ }
60
+
61
+ @ Test
62
+ public void configurationIncludesAspect () {
63
+ assertAdviceWasApplied (ConfigurationWithAspect .class );
64
+ }
65
+
49
66
private void assertAdviceWasApplied (Class <?> configClass ) {
50
67
DefaultListableBeanFactory factory = new DefaultListableBeanFactory ();
51
68
new XmlBeanDefinitionReader (factory ).loadBeanDefinitions (
@@ -62,19 +79,17 @@ private void assertAdviceWasApplied(Class<?> configClass) {
62
79
}
63
80
64
81
@ Test
65
- public void aspectAnnotatedConfiguration () {
66
- assertAdviceWasApplied (AspectConfig .class );
67
- }
68
-
69
- @ Test
70
- public void configurationIncludesAspect () {
71
- assertAdviceWasApplied (ConfigurationWithAspect .class );
82
+ public void withInnerClassAndLambdaExpression () {
83
+ ApplicationContext ctx = new AnnotationConfigApplicationContext (Application .class , CountingAspect .class );
84
+ ctx .getBeansOfType (Runnable .class ).forEach ((k , v ) -> v .run ());
85
+ assertEquals (2 , ctx .getBean (CountingAspect .class ).count );
72
86
}
73
87
74
88
75
89
@ Aspect
76
90
@ Configuration
77
91
static class AspectConfig {
92
+
78
93
@ Bean
79
94
public TestBean testBean () {
80
95
return new TestBean ("name" );
@@ -86,8 +101,10 @@ public void touchBean(TestBean testBean) {
86
101
}
87
102
}
88
103
104
+
89
105
@ Configuration
90
106
static class ConfigurationWithAspect {
107
+
91
108
@ Bean
92
109
public TestBean testBean () {
93
110
return new TestBean ("name" );
@@ -99,11 +116,48 @@ public NameChangingAspect nameChangingAspect() {
99
116
}
100
117
}
101
118
119
+
102
120
@ Aspect
103
121
static class NameChangingAspect {
122
+
104
123
@ Before ("execution(* org.springframework.tests.sample.beans.TestBean.absquatulate(..)) && target(testBean)" )
105
124
public void touchBean (TestBean testBean ) {
106
125
testBean .setName ("advisedName" );
107
126
}
108
127
}
128
+
129
+
130
+
131
+ @ Configuration
132
+ @ EnableAspectJAutoProxy
133
+ public static class Application {
134
+
135
+ @ Bean
136
+ Runnable fromInnerClass () {
137
+ return new Runnable () {
138
+ @ Override
139
+ public void run () {
140
+ }
141
+ };
142
+ }
143
+
144
+ @ Bean
145
+ Runnable fromLambdaExpression () {
146
+ return () -> {
147
+ };
148
+ }
149
+ }
150
+
151
+
152
+ @ Aspect
153
+ public static class CountingAspect {
154
+
155
+ public int count = 0 ;
156
+
157
+ @ After ("execution(* java.lang.Runnable.*(..))" )
158
+ public void after (JoinPoint joinPoint ) {
159
+ count ++;
160
+ }
161
+ }
162
+
109
163
}
0 commit comments