Skip to content

Commit d2621ba

Browse files
committed
Enabled test for AspectJ pointcut matching against lambda-defined bean
Issue: SPR-11807 (cherry picked from commit b02352d)
1 parent 7a6ce1c commit d2621ba

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

spring-context/src/test/java/org/springframework/context/annotation/configuration/ConfigurationClassAspectIntegrationTests.java

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2015 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,16 +16,21 @@
1616

1717
package org.springframework.context.annotation.configuration;
1818

19+
import org.aspectj.lang.JoinPoint;
20+
import org.aspectj.lang.annotation.After;
1921
import org.aspectj.lang.annotation.Aspect;
2022
import org.aspectj.lang.annotation.Before;
2123
import org.junit.Test;
2224

2325
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2426
import org.springframework.beans.factory.support.RootBeanDefinition;
2527
import org.springframework.beans.factory.xml.XmlBeanDefinitionReader;
28+
import org.springframework.context.ApplicationContext;
29+
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
2630
import org.springframework.context.annotation.Bean;
2731
import org.springframework.context.annotation.Configuration;
2832
import org.springframework.context.annotation.ConfigurationClassPostProcessor;
33+
import org.springframework.context.annotation.EnableAspectJAutoProxy;
2934
import org.springframework.context.support.GenericApplicationContext;
3035
import org.springframework.core.io.ClassPathResource;
3136
import org.springframework.tests.sample.beans.TestBean;
@@ -44,8 +49,20 @@
4449
* processing of the Aspect annotation.
4550
*
4651
* @author Chris Beams
52+
* @author Juergen Hoeller
4753
*/
4854
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+
4966
private void assertAdviceWasApplied(Class<?> configClass) {
5067
DefaultListableBeanFactory factory = new DefaultListableBeanFactory();
5168
new XmlBeanDefinitionReader(factory).loadBeanDefinitions(
@@ -62,19 +79,17 @@ private void assertAdviceWasApplied(Class<?> configClass) {
6279
}
6380

6481
@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);
7286
}
7387

7488

7589
@Aspect
7690
@Configuration
7791
static class AspectConfig {
92+
7893
@Bean
7994
public TestBean testBean() {
8095
return new TestBean("name");
@@ -86,8 +101,10 @@ public void touchBean(TestBean testBean) {
86101
}
87102
}
88103

104+
89105
@Configuration
90106
static class ConfigurationWithAspect {
107+
91108
@Bean
92109
public TestBean testBean() {
93110
return new TestBean("name");
@@ -99,11 +116,48 @@ public NameChangingAspect nameChangingAspect() {
99116
}
100117
}
101118

119+
102120
@Aspect
103121
static class NameChangingAspect {
122+
104123
@Before("execution(* org.springframework.tests.sample.beans.TestBean.absquatulate(..)) && target(testBean)")
105124
public void touchBean(TestBean testBean) {
106125
testBean.setName("advisedName");
107126
}
108127
}
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+
109163
}

0 commit comments

Comments
 (0)