Skip to content

Commit ae5f2d7

Browse files
committed
Do not require aspectjweaver to enable CGLib-based proxies
Previously, AopAutoConfiguration would only enable CGLib-based proxies if aspectjweaver was on the classpath. The intention was for CGLib-based proxies to always be used by default so this behaviour was incorrect. This commit updates AopAutoConfiguration to force the use of CGLib-based proxies even in the absence of aspectjweaver. Closes gh-18523
1 parent 07ca774 commit ae5f2d7

File tree

2 files changed

+91
-10
lines changed

2 files changed

+91
-10
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/aop/AopAutoConfiguration.java

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
package org.springframework.boot.autoconfigure.aop;
1818

19-
import org.aspectj.lang.annotation.Aspect;
20-
import org.aspectj.lang.reflect.Advice;
21-
import org.aspectj.weaver.AnnotatedElement;
19+
import org.aspectj.weaver.Advice;
2220

21+
import org.springframework.aop.config.AopConfigUtils;
22+
import org.springframework.beans.factory.BeanFactory;
23+
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2324
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
2426
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2527
import org.springframework.context.annotation.Configuration;
2628
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@@ -40,23 +42,44 @@
4042
* @see EnableAspectJAutoProxy
4143
*/
4244
@Configuration(proxyBeanMethods = false)
43-
@ConditionalOnClass({ EnableAspectJAutoProxy.class, Aspect.class, Advice.class, AnnotatedElement.class })
4445
@ConditionalOnProperty(prefix = "spring.aop", name = "auto", havingValue = "true", matchIfMissing = true)
4546
public class AopAutoConfiguration {
4647

4748
@Configuration(proxyBeanMethods = false)
48-
@EnableAspectJAutoProxy(proxyTargetClass = false)
49-
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",
50-
matchIfMissing = false)
51-
public static class JdkDynamicAutoProxyConfiguration {
49+
@ConditionalOnClass(Advice.class)
50+
static class AspectJAutoProxyingConfiguration {
51+
52+
@Configuration(proxyBeanMethods = false)
53+
@EnableAspectJAutoProxy(proxyTargetClass = false)
54+
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "false",
55+
matchIfMissing = false)
56+
static class JdkDynamicAutoProxyConfiguration {
57+
58+
}
59+
60+
@Configuration(proxyBeanMethods = false)
61+
@EnableAspectJAutoProxy(proxyTargetClass = true)
62+
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
63+
matchIfMissing = true)
64+
static class CglibAutoProxyConfiguration {
65+
66+
}
5267

5368
}
5469

5570
@Configuration(proxyBeanMethods = false)
56-
@EnableAspectJAutoProxy(proxyTargetClass = true)
71+
@ConditionalOnMissingClass("org.aspectj.weaver.Advice")
5772
@ConditionalOnProperty(prefix = "spring.aop", name = "proxy-target-class", havingValue = "true",
5873
matchIfMissing = true)
59-
public static class CglibAutoProxyConfiguration {
74+
static class ClassProxyingConfiguration {
75+
76+
ClassProxyingConfiguration(BeanFactory beanFactory) {
77+
if (beanFactory instanceof BeanDefinitionRegistry) {
78+
BeanDefinitionRegistry registry = (BeanDefinitionRegistry) beanFactory;
79+
AopConfigUtils.registerAutoProxyCreatorIfNecessary(registry);
80+
AopConfigUtils.forceAutoProxyCreatorToUseClassProxying(registry);
81+
}
82+
}
6083

6184
}
6285

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.aop;
18+
19+
import org.junit.jupiter.api.Test;
20+
21+
import org.springframework.aop.config.AopConfigUtils;
22+
import org.springframework.beans.factory.config.BeanDefinition;
23+
import org.springframework.boot.autoconfigure.AutoConfigurations;
24+
import org.springframework.boot.autoconfigure.logging.ConditionEvaluationReportLoggingListener;
25+
import org.springframework.boot.logging.LogLevel;
26+
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
27+
import org.springframework.boot.testsupport.classpath.ClassPathExclusions;
28+
29+
import static org.assertj.core.api.Assertions.assertThat;
30+
31+
/**
32+
* Tests for {@link AopAutoConfiguration} without AspectJ.
33+
*
34+
* @author Andy Wilkinson
35+
*/
36+
@ClassPathExclusions("aspectjweaver*.jar")
37+
class NonAspectJAopAutoConfigurationTests {
38+
39+
private final ApplicationContextRunner contextRunner = new ApplicationContextRunner()
40+
.withConfiguration(AutoConfigurations.of(AopAutoConfiguration.class));
41+
42+
@Test
43+
void whenAspectJIsAbsentAndProxyTargetClassIsEnabledProxyCreatorBeanIsDefined() {
44+
this.contextRunner.withInitializer(new ConditionEvaluationReportLoggingListener(LogLevel.INFO))
45+
.run((context) -> {
46+
BeanDefinition autoProxyCreator = context.getBeanFactory()
47+
.getBeanDefinition(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME);
48+
assertThat(autoProxyCreator.getPropertyValues().get("proxyTargetClass")).isEqualTo(Boolean.TRUE);
49+
});
50+
}
51+
52+
@Test
53+
void whenAspectJIsAbsentAndProxyTargetClassIsDisabledNoProxyCreatorBeanIsDefined() {
54+
this.contextRunner.withPropertyValues("spring.aop.proxy-target-class:false")
55+
.run((context) -> assertThat(context).doesNotHaveBean(AopConfigUtils.AUTO_PROXY_CREATOR_BEAN_NAME));
56+
}
57+
58+
}

0 commit comments

Comments
 (0)