Skip to content

Commit a2b9671

Browse files
committed
Merge pull request #47429 from nosan
* pr/47429: Stop using @ConditionalOnClass on @bean methods Closes gh-47429
2 parents 4e4d3da + ba88ec7 commit a2b9671

File tree

14 files changed

+288
-84
lines changed

14 files changed

+288
-84
lines changed

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,19 @@
7070
*/
7171
public abstract class ArchitectureCheck extends DefaultTask {
7272

73+
private static final String CONDITIONAL_ON_CLASS_ANNOTATION = "org.springframework.boot.autoconfigure.condition.ConditionalOnClass";
74+
7375
private FileCollection classes;
7476

7577
public ArchitectureCheck() {
7678
getOutputDirectory().convention(getProject().getLayout().getBuildDirectory().dir(getName()));
79+
getConditionalOnClassAnnotation().convention(CONDITIONAL_ON_CLASS_ANNOTATION);
7780
getRules().addAll(getProhibitObjectsRequireNonNull().convention(true)
7881
.map(whenTrue(ArchitectureRules::noClassesShouldCallObjectsRequireNonNull)));
7982
getRules().addAll(ArchitectureRules.standard());
80-
getRules().addAll(whenMainSources(
81-
() -> Collections.singletonList(ArchitectureRules.allBeanMethodsShouldReturnNonPrivateType())));
83+
getRules().addAll(whenMainSources(() -> List
84+
.of(ArchitectureRules.allBeanMethodsShouldReturnNonPrivateType(), ArchitectureRules
85+
.allBeanMethodsShouldNotHaveConditionalOnClassAnnotation(getConditionalOnClassAnnotation().get()))));
8286
getRuleDescriptions().set(getRules().map(this::asDescriptions));
8387
}
8488

@@ -186,4 +190,7 @@ final FileTree getInputClasses() {
186190
@Input // Use descriptions as input since rules aren't serializable
187191
abstract ListProperty<String> getRuleDescriptions();
188192

193+
@Internal
194+
abstract Property<String> getConditionalOnClassAnnotation();
195+
189196
}

buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureRules.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ static ArchRule allBeanMethodsShouldReturnNonPrivateType() {
108108
.allowEmptyShould(true);
109109
}
110110

111+
static ArchRule allBeanMethodsShouldNotHaveConditionalOnClassAnnotation(String annotationName) {
112+
return methodsThatAreAnnotatedWith("org.springframework.context.annotation.Bean").should()
113+
.notBeAnnotatedWith(annotationName)
114+
.because("@ConditionalOnClass on @Bean methods is ineffective - it doesn't prevent "
115+
+ "the method signature from being loaded. Such condition need to be placed"
116+
+ " on a @Configuration class, allowing the condition to back off before the type is loaded.")
117+
.allowEmptyShould(true);
118+
}
119+
111120
private static ArchRule allPackagesShouldBeFreeOfTangles() {
112121
return SlicesRuleDefinition.slices().matching("(**)").should().beFreeOfCycles();
113122
}

buildSrc/src/test/java/org/springframework/boot/build/architecture/ArchitectureCheckTests.java

Lines changed: 68 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.LinkedHashSet;
2727
import java.util.Map;
2828
import java.util.Set;
29+
import java.util.function.UnaryOperator;
2930

3031
import org.gradle.api.tasks.SourceSet;
3132
import org.gradle.testkit.runner.BuildResult;
@@ -39,6 +40,7 @@
3940
import org.junit.jupiter.params.ParameterizedTest;
4041
import org.junit.jupiter.params.provider.EnumSource;
4142

43+
import org.springframework.boot.build.architecture.annotations.TestConditionalOnClass;
4244
import org.springframework.util.ClassUtils;
4345
import org.springframework.util.FileSystemUtils;
4446
import org.springframework.util.StringUtils;
@@ -180,7 +182,7 @@ void whenClassCallsObjectsRequireNonNullWithMessageShouldFailAndWriteReport(Task
180182
void whenClassCallsObjectsRequireNonNullWithMessageAndProhibitObjectsRequireNonNullIsFalseShouldSucceedAndWriteEmptyReport(
181183
Task task) throws IOException {
182184
prepareTask(task, "objects/requireNonNullWithString");
183-
build(this.gradleBuild.withProhibitObjectsRequireNonNull(task, false), task);
185+
build(this.gradleBuild.withProhibitObjectsRequireNonNull(false), task);
184186
}
185187

186188
@ParameterizedTest(name = "{0}")
@@ -195,7 +197,7 @@ void whenClassCallsObjectsRequireNonNullWithSupplierShouldFailAndWriteReport(Tas
195197
void whenClassCallsObjectsRequireNonNullWithSupplierAndProhibitObjectsRequireNonNullIsFalseShouldSucceedAndWriteEmptyReport(
196198
Task task) throws IOException {
197199
prepareTask(task, "objects/requireNonNullWithSupplier");
198-
build(this.gradleBuild.withProhibitObjectsRequireNonNull(task, false), task);
200+
build(this.gradleBuild.withProhibitObjectsRequireNonNull(false), task);
199201
}
200202

201203
@ParameterizedTest(name = "{0}")
@@ -295,6 +297,25 @@ void whenEnumSourceValueIsSameAsTypeOfMethodsFirstParameterShouldFailAndWriteRep
295297
"should not have a value that is the same as the type of the method's first parameter");
296298
}
297299

300+
@Test
301+
void whenConditionalOnClassUsedOnBeanMethodsWithMainSourcesShouldFailAndWriteReport() throws IOException {
302+
prepareTask(Task.CHECK_ARCHITECTURE_MAIN, "conditionalonclass", "annotations");
303+
GradleBuild gradleBuild = this.gradleBuild.withDependencies(SPRING_CONTEXT)
304+
.withConditionalOnClassAnnotation(TestConditionalOnClass.class.getName());
305+
buildAndFail(gradleBuild, Task.CHECK_ARCHITECTURE_MAIN,
306+
"because @ConditionalOnClass on @Bean methods is ineffective - it doesn't prevent"
307+
+ " the method signature from being loaded. Such condition need to be placed"
308+
+ " on a @Configuration class, allowing the condition to back off before the type is loaded");
309+
}
310+
311+
@Test
312+
void whenConditionalOnClassUsedOnBeanMethodsWithTestSourcesShouldSucceedAndWriteEmptyReport() throws IOException {
313+
prepareTask(Task.CHECK_ARCHITECTURE_TEST, "conditionalonclass", "annotations");
314+
GradleBuild gradleBuild = this.gradleBuild.withDependencies(SPRING_CONTEXT)
315+
.withConditionalOnClassAnnotation(TestConditionalOnClass.class.getName());
316+
build(gradleBuild, Task.CHECK_ARCHITECTURE_TEST);
317+
}
318+
298319
private void prepareTask(Task task, String... sourceDirectories) throws IOException {
299320
for (String sourceDirectory : sourceDirectories) {
300321
FileSystemUtils.copyRecursively(
@@ -310,7 +331,7 @@ private void prepareTask(Task task, String... sourceDirectories) throws IOExcept
310331
private void build(GradleBuild gradleBuild, Task task) throws IOException {
311332
try {
312333
BuildResult buildResult = gradleBuild.build(task.toString());
313-
assertThat(buildResult.taskPaths(TaskOutcome.SUCCESS)).contains(":" + task);
334+
assertThat(buildResult.taskPaths(TaskOutcome.SUCCESS)).as(buildResult.getOutput()).contains(":" + task);
314335
assertThat(task.getFailureReport(gradleBuild.getProjectDir())).isEmpty();
315336
}
316337
catch (UnexpectedBuildFailure ex) {
@@ -326,7 +347,7 @@ private void build(GradleBuild gradleBuild, Task task) throws IOException {
326347
private void buildAndFail(GradleBuild gradleBuild, Task task, String... messages) throws IOException {
327348
try {
328349
BuildResult buildResult = gradleBuild.buildAndFail(task.toString());
329-
assertThat(buildResult.taskPaths(TaskOutcome.FAILED)).contains(":" + task);
350+
assertThat(buildResult.taskPaths(TaskOutcome.FAILED)).as(buildResult.getOutput()).contains(":" + task);
330351
assertThat(task.getFailureReport(gradleBuild.getProjectDir())).contains(messages);
331352
}
332353
catch (UnexpectedBuildSuccess ex) {
@@ -371,7 +392,7 @@ private static final class GradleBuild {
371392

372393
private final Set<String> dependencies = new LinkedHashSet<>();
373394

374-
private final Map<Task, Boolean> prohibitObjectsRequireNonNull = new LinkedHashMap<>();
395+
private final Map<Task, TaskConfiguration> taskConfigurations = new LinkedHashMap<>();
375396

376397
private GradleBuild(Path projectDir) {
377398
this.projectDir = projectDir;
@@ -381,12 +402,28 @@ Path getProjectDir() {
381402
return this.projectDir;
382403
}
383404

384-
GradleBuild withProhibitObjectsRequireNonNull(Task task, boolean prohibitObjectsRequireNonNull) {
385-
this.prohibitObjectsRequireNonNull.put(task, prohibitObjectsRequireNonNull);
405+
GradleBuild withProhibitObjectsRequireNonNull(Boolean prohibitObjectsRequireNonNull) {
406+
for (Task task : Task.values()) {
407+
configureTask(task, (configuration) -> configuration
408+
.withProhibitObjectsRequireNonNull(prohibitObjectsRequireNonNull));
409+
}
410+
return this;
411+
}
412+
413+
GradleBuild withConditionalOnClassAnnotation(String annotationName) {
414+
for (Task task : Task.values()) {
415+
configureTask(task, (configuration) -> configuration.withConditionalOnClassAnnotation(annotationName));
416+
}
386417
return this;
387418
}
388419

420+
private void configureTask(Task task, UnaryOperator<TaskConfiguration> configurer) {
421+
this.taskConfigurations.computeIfAbsent(task, (key) -> new TaskConfiguration(null, null));
422+
this.taskConfigurations.compute(task, (key, value) -> configurer.apply(value));
423+
}
424+
389425
GradleBuild withDependencies(String... dependencies) {
426+
this.dependencies.clear();
390427
this.dependencies.addAll(Arrays.asList(dependencies));
391428
return this;
392429
}
@@ -415,22 +452,40 @@ private GradleRunner prepareRunner(String... arguments) throws IOException {
415452
if (!this.dependencies.isEmpty()) {
416453
buildFile.append("dependencies {\n");
417454
for (String dependency : this.dependencies) {
418-
buildFile.append(" implementation '%s'\n".formatted(dependency));
455+
buildFile.append("\n implementation ").append(StringUtils.quote(dependency));
419456
}
420457
buildFile.append("}\n");
421458
}
422-
this.prohibitObjectsRequireNonNull.forEach((task, prohibitObjectsRequireNonNull) -> buildFile.append(task)
423-
.append(" {\n")
424-
.append(" prohibitObjectsRequireNonNull = ")
425-
.append(prohibitObjectsRequireNonNull)
426-
.append("\n}\n\n"));
459+
this.taskConfigurations.forEach((task, configuration) -> {
460+
buildFile.append(task).append(" {");
461+
if (configuration.conditionalOnClassAnnotation() != null) {
462+
buildFile.append("\n conditionalOnClassAnnotation = ")
463+
.append(StringUtils.quote(configuration.conditionalOnClassAnnotation()));
464+
}
465+
if (configuration.prohibitObjectsRequireNonNull() != null) {
466+
buildFile.append("\n prohibitObjectsRequireNonNull = ")
467+
.append(configuration.prohibitObjectsRequireNonNull());
468+
}
469+
buildFile.append("\n}\n");
470+
});
427471
Files.writeString(this.projectDir.resolve("build.gradle"), buildFile, StandardCharsets.UTF_8);
428472
return GradleRunner.create()
429473
.withProjectDir(this.projectDir.toFile())
430474
.withArguments(arguments)
431475
.withPluginClasspath();
432476
}
433477

478+
private record TaskConfiguration(Boolean prohibitObjectsRequireNonNull, String conditionalOnClassAnnotation) {
479+
480+
private TaskConfiguration withConditionalOnClassAnnotation(String annotationName) {
481+
return new TaskConfiguration(this.prohibitObjectsRequireNonNull, annotationName);
482+
}
483+
484+
private TaskConfiguration withProhibitObjectsRequireNonNull(Boolean prohibitObjectsRequireNonNull) {
485+
return new TaskConfiguration(prohibitObjectsRequireNonNull, this.conditionalOnClassAnnotation);
486+
}
487+
}
488+
434489
}
435490

436491
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/*
2+
* Copyright 2012-present 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.build.architecture.annotations;
18+
19+
import java.lang.annotation.ElementType;
20+
import java.lang.annotation.Retention;
21+
import java.lang.annotation.RetentionPolicy;
22+
import java.lang.annotation.Target;
23+
24+
/**
25+
* {@code @ConditionalOnClass} analogue for architecture checks.
26+
*
27+
*/
28+
@Target({ ElementType.TYPE, ElementType.METHOD })
29+
@Retention(RetentionPolicy.RUNTIME)
30+
public @interface TestConditionalOnClass {
31+
32+
Class<?>[] value() default {};
33+
34+
String[] name() default {};
35+
36+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* Copyright 2012-present 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.build.architecture.conditionalonclass;
18+
19+
import org.springframework.boot.build.architecture.annotations.TestConditionalOnClass;
20+
import org.springframework.context.annotation.Bean;
21+
22+
class OnBeanMethod {
23+
24+
@Bean
25+
@TestConditionalOnClass(String.class)
26+
String helloWorld() {
27+
return "Hello World";
28+
}
29+
30+
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/audit/AuditAutoConfiguration.java

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
3232
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
3333
import org.springframework.context.annotation.Bean;
34+
import org.springframework.context.annotation.Configuration;
3435

3536
/**
3637
* {@link EnableAutoConfiguration Auto-configuration} for {@link AuditEvent}s.
@@ -50,18 +51,28 @@ public AuditListener auditListener(AuditEventRepository auditEventRepository) {
5051
return new AuditListener(auditEventRepository);
5152
}
5253

53-
@Bean
54+
@Configuration(proxyBeanMethods = false)
5455
@ConditionalOnClass(name = "org.springframework.security.authentication.event.AbstractAuthenticationEvent")
55-
@ConditionalOnMissingBean(AbstractAuthenticationAuditListener.class)
56-
public AuthenticationAuditListener authenticationAuditListener() {
57-
return new AuthenticationAuditListener();
56+
static class AuthenticationAuditConfiguration {
57+
58+
@Bean
59+
@ConditionalOnMissingBean(AbstractAuthenticationAuditListener.class)
60+
AuthenticationAuditListener authenticationAuditListener() {
61+
return new AuthenticationAuditListener();
62+
}
63+
5864
}
5965

60-
@Bean
66+
@Configuration(proxyBeanMethods = false)
6167
@ConditionalOnClass(name = "org.springframework.security.access.event.AbstractAuthorizationEvent")
62-
@ConditionalOnMissingBean(AbstractAuthorizationAuditListener.class)
63-
public AuthorizationAuditListener authorizationAuditListener() {
64-
return new AuthorizationAuditListener();
68+
static class AuthorizationAuditConfiguration {
69+
70+
@Bean
71+
@ConditionalOnMissingBean(AbstractAuthorizationAuditListener.class)
72+
AuthorizationAuditListener authorizationAuditListener() {
73+
return new AuthorizationAuditListener();
74+
}
75+
6576
}
6677

6778
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/endpoint/jackson/JacksonEndpointAutoConfiguration.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,12 @@
3636
* @since 3.0.0
3737
*/
3838
@AutoConfiguration(after = JacksonAutoConfiguration.class)
39+
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
3940
public class JacksonEndpointAutoConfiguration {
4041

4142
@Bean
4243
@ConditionalOnProperty(name = "management.endpoints.jackson.isolated-object-mapper", matchIfMissing = true)
43-
@ConditionalOnClass({ ObjectMapper.class, Jackson2ObjectMapperBuilder.class })
44-
public EndpointObjectMapper endpointObjectMapper() {
44+
EndpointObjectMapper endpointObjectMapper() {
4545
ObjectMapper objectMapper = Jackson2ObjectMapperBuilder.json()
4646
.featuresToDisable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS,
4747
SerializationFeature.WRITE_DURATIONS_AS_TIMESTAMPS)

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/security/servlet/SecurityRequestMatchersManagementContextConfiguration.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ public static class MvcRequestMatcherConfiguration {
5353

5454
@Bean
5555
@ConditionalOnMissingBean
56-
@ConditionalOnClass(DispatcherServlet.class)
5756
public RequestMatcherProvider requestMatcherProvider(DispatcherServletPath servletPath) {
5857
return new AntPathRequestMatcherProvider(servletPath::getRelativePath);
5958
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/web/servlet/ServletManagementChildContextConfiguration.java

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,22 +81,37 @@ ServletManagementWebServerFactoryCustomizer servletManagementWebServerFactoryCus
8181
return new ServletManagementWebServerFactoryCustomizer(beanFactory);
8282
}
8383

84-
@Bean
84+
@Configuration(proxyBeanMethods = false)
8585
@ConditionalOnClass(name = "io.undertow.Undertow")
86-
UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer() {
87-
return new UndertowAccessLogCustomizer();
86+
static class UndertowConfiguration {
87+
88+
@Bean
89+
UndertowAccessLogCustomizer undertowManagementAccessLogCustomizer() {
90+
return new UndertowAccessLogCustomizer();
91+
}
92+
8893
}
8994

90-
@Bean
95+
@Configuration(proxyBeanMethods = false)
9196
@ConditionalOnClass(name = "org.apache.catalina.valves.AccessLogValve")
92-
TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer() {
93-
return new TomcatAccessLogCustomizer();
97+
static class TomcatConfiguration {
98+
99+
@Bean
100+
TomcatAccessLogCustomizer tomcatManagementAccessLogCustomizer() {
101+
return new TomcatAccessLogCustomizer();
102+
}
103+
94104
}
95105

96-
@Bean
106+
@Configuration(proxyBeanMethods = false)
97107
@ConditionalOnClass(name = "org.eclipse.jetty.server.Server")
98-
JettyAccessLogCustomizer jettyManagementAccessLogCustomizer() {
99-
return new JettyAccessLogCustomizer();
108+
static class JettyConfiguration {
109+
110+
@Bean
111+
JettyAccessLogCustomizer jettyManagementAccessLogCustomizer() {
112+
return new JettyAccessLogCustomizer();
113+
}
114+
100115
}
101116

102117
@Configuration(proxyBeanMethods = false)

0 commit comments

Comments
 (0)