Skip to content

Commit 7cd497f

Browse files
committed
Stop using @ConditionalOnClass on @bean methods
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent f85c743 commit 7cd497f

File tree

14 files changed

+279
-80
lines changed

14 files changed

+279
-80
lines changed

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

Lines changed: 8 additions & 1 deletion
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());
8083
getRules().addAll(whenMainSources(
81-
() -> Collections.singletonList(ArchitectureRules.allBeanMethodsShouldReturnNonPrivateType())));
84+
() -> List.of(ArchitectureRules.allBeanMethodsShouldReturnNonPrivateType(), ArchitectureRules
85+
.allBeanMethodsShouldNotHaveConditionalOnClass(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 allBeanMethodsShouldNotHaveConditionalOnClass(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: 60 additions & 10 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;
@@ -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(Task.CHECK_ARCHITECTURE_MAIN, 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(Task.CHECK_ARCHITECTURE_TEST, 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,8 +402,19 @@ Path getProjectDir() {
381402
return this.projectDir;
382403
}
383404

384-
GradleBuild withProhibitObjectsRequireNonNull(Task task, boolean prohibitObjectsRequireNonNull) {
385-
this.prohibitObjectsRequireNonNull.put(task, prohibitObjectsRequireNonNull);
405+
GradleBuild withProhibitObjectsRequireNonNull(Task task, Boolean prohibitObjectsRequireNonNull) {
406+
return withTaskConfiguration(task,
407+
(configuration) -> configuration.withProhibitObjectsRequireNonNull(prohibitObjectsRequireNonNull));
408+
}
409+
410+
GradleBuild withConditionalOnClassAnnotation(Task task, String annotationName) {
411+
return withTaskConfiguration(task,
412+
(configuration) -> configuration.withConditionalOnClassAnnotation(annotationName));
413+
}
414+
415+
GradleBuild withTaskConfiguration(Task task, UnaryOperator<TaskConfiguration> customizer) {
416+
this.taskConfigurations.computeIfAbsent(task, (key) -> new TaskConfiguration(null, null));
417+
this.taskConfigurations.compute(task, (key, value) -> customizer.apply(value));
386418
return this;
387419
}
388420

@@ -419,18 +451,36 @@ private GradleRunner prepareRunner(String... arguments) throws IOException {
419451
}
420452
buildFile.append("}\n");
421453
}
422-
this.prohibitObjectsRequireNonNull.forEach((task, prohibitObjectsRequireNonNull) -> buildFile.append(task)
423-
.append(" {\n")
424-
.append(" prohibitObjectsRequireNonNull = ")
425-
.append(prohibitObjectsRequireNonNull)
426-
.append("\n}\n\n"));
454+
this.taskConfigurations.forEach((task, configuration) -> {
455+
buildFile.append(task).append(" {");
456+
if (configuration.conditionalOnClassAnnotation() != null) {
457+
buildFile.append("\n conditionalOnClassAnnotation = ")
458+
.append(StringUtils.quote(configuration.conditionalOnClassAnnotation()));
459+
}
460+
if (configuration.prohibitObjectsRequireNonNull() != null) {
461+
buildFile.append("\n prohibitObjectsRequireNonNull = ")
462+
.append(configuration.prohibitObjectsRequireNonNull());
463+
}
464+
buildFile.append("\n}\n");
465+
});
427466
Files.writeString(this.projectDir.resolve("build.gradle"), buildFile, StandardCharsets.UTF_8);
428467
return GradleRunner.create()
429468
.withProjectDir(this.projectDir.toFile())
430469
.withArguments(arguments)
431470
.withPluginClasspath();
432471
}
433472

473+
private record TaskConfiguration(Boolean prohibitObjectsRequireNonNull, String conditionalOnClassAnnotation) {
474+
475+
private TaskConfiguration withConditionalOnClassAnnotation(String annotationName) {
476+
return new TaskConfiguration(this.prohibitObjectsRequireNonNull, annotationName);
477+
}
478+
479+
private TaskConfiguration withProhibitObjectsRequireNonNull(Boolean prohibitObjectsRequireNonNull) {
480+
return new TaskConfiguration(prohibitObjectsRequireNonNull, this.conditionalOnClassAnnotation);
481+
}
482+
}
483+
434484
}
435485

436486
}
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)