Skip to content

Commit 4dc204e

Browse files
committed
Stop using @ConditionalOnClass on @bean methods
Signed-off-by: Dmytro Nosan <[email protected]>
1 parent 3ba61c3 commit 4dc204e

File tree

14 files changed

+267
-86
lines changed

14 files changed

+267
-86
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+
getConditionalOnClassAnnotationName().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(getConditionalOnClassAnnotationName().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> getConditionalOnClassAnnotationName();
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 conditionalOnClassAnnotationName) {
112+
return methodsThatAreAnnotatedWith("org.springframework.context.annotation.Bean").should()
113+
.notBeAnnotatedWith(conditionalOnClassAnnotationName)
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: 49 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@
2222
import java.nio.file.Path;
2323
import java.nio.file.Paths;
2424
import java.util.Arrays;
25-
import java.util.LinkedHashMap;
2625
import java.util.LinkedHashSet;
27-
import java.util.Map;
2826
import java.util.Set;
2927

3028
import org.gradle.api.tasks.SourceSet;
@@ -39,6 +37,7 @@
3937
import org.junit.jupiter.params.ParameterizedTest;
4038
import org.junit.jupiter.params.provider.EnumSource;
4139

40+
import org.springframework.boot.build.architecture.annotations.ArchitectureCheckConditionalOnClass;
4241
import org.springframework.util.ClassUtils;
4342
import org.springframework.util.FileSystemUtils;
4443
import org.springframework.util.StringUtils;
@@ -180,7 +179,7 @@ void whenClassCallsObjectsRequireNonNullWithMessageShouldFailAndWriteReport(Task
180179
void whenClassCallsObjectsRequireNonNullWithMessageAndProhibitObjectsRequireNonNullIsFalseShouldSucceedAndWriteEmptyReport(
181180
Task task) throws IOException {
182181
prepareTask(task, "objects/requireNonNullWithString");
183-
build(this.gradleBuild.withProhibitObjectsRequireNonNull(task, false), task);
182+
build(this.gradleBuild.withProhibitObjectsRequireNonNull(false), task);
184183
}
185184

186185
@ParameterizedTest(name = "{0}")
@@ -195,7 +194,7 @@ void whenClassCallsObjectsRequireNonNullWithSupplierShouldFailAndWriteReport(Tas
195194
void whenClassCallsObjectsRequireNonNullWithSupplierAndProhibitObjectsRequireNonNullIsFalseShouldSucceedAndWriteEmptyReport(
196195
Task task) throws IOException {
197196
prepareTask(task, "objects/requireNonNullWithSupplier");
198-
build(this.gradleBuild.withProhibitObjectsRequireNonNull(task, false), task);
197+
build(this.gradleBuild.withProhibitObjectsRequireNonNull(false), task);
199198
}
200199

201200
@ParameterizedTest(name = "{0}")
@@ -295,6 +294,25 @@ void whenEnumSourceValueIsSameAsTypeOfMethodsFirstParameterShouldFailAndWriteRep
295294
"should not have a value that is the same as the type of the method's first parameter");
296295
}
297296

297+
@Test
298+
void whenConditionalOnClassUsedOnBeanMethodsWithMainSourcesShouldFailAndWriteReport() throws IOException {
299+
prepareTask(Task.CHECK_ARCHITECTURE_MAIN, "conditionalonclass", "annotations");
300+
GradleBuild gradleBuild = this.gradleBuild.withDependencies(SPRING_CONTEXT)
301+
.withConditionalOnClassAnnotation(ArchitectureCheckConditionalOnClass.class.getName());
302+
buildAndFail(gradleBuild, Task.CHECK_ARCHITECTURE_MAIN,
303+
"because @ConditionalOnClass on @Bean methods is ineffective - it doesn't prevent"
304+
+ " the method signature from being loaded. Such condition need to be placed"
305+
+ " on a @Configuration class, allowing the condition to back off before the type is loaded");
306+
}
307+
308+
@Test
309+
void whenConditionalOnClassUsedOnBeanMethodsWithTestSourcesShouldSucceedAndWriteEmptyReport() throws IOException {
310+
prepareTask(Task.CHECK_ARCHITECTURE_TEST, "conditionalonclass", "annotations");
311+
GradleBuild gradleBuild = this.gradleBuild.withDependencies(SPRING_CONTEXT)
312+
.withConditionalOnClassAnnotation(ArchitectureCheckConditionalOnClass.class.getName());
313+
build(gradleBuild, Task.CHECK_ARCHITECTURE_TEST);
314+
}
315+
298316
private void prepareTask(Task task, String... sourceDirectories) throws IOException {
299317
for (String sourceDirectory : sourceDirectories) {
300318
FileSystemUtils.copyRecursively(
@@ -310,7 +328,8 @@ private void prepareTask(Task task, String... sourceDirectories) throws IOExcept
310328
private void build(GradleBuild gradleBuild, Task task) throws IOException {
311329
try {
312330
BuildResult buildResult = gradleBuild.build(task.toString());
313-
assertThat(buildResult.taskPaths(TaskOutcome.SUCCESS)).contains(":" + task);
331+
assertThat(buildResult.taskPaths(TaskOutcome.SUCCESS)).describedAs(buildResult.getOutput())
332+
.contains(":" + task);
314333
assertThat(task.getFailureReport(gradleBuild.getProjectDir())).isEmpty();
315334
}
316335
catch (UnexpectedBuildFailure ex) {
@@ -326,7 +345,8 @@ private void build(GradleBuild gradleBuild, Task task) throws IOException {
326345
private void buildAndFail(GradleBuild gradleBuild, Task task, String... messages) throws IOException {
327346
try {
328347
BuildResult buildResult = gradleBuild.buildAndFail(task.toString());
329-
assertThat(buildResult.taskPaths(TaskOutcome.FAILED)).contains(":" + task);
348+
assertThat(buildResult.taskPaths(TaskOutcome.FAILED)).describedAs(buildResult.getOutput())
349+
.contains(":" + task);
330350
assertThat(task.getFailureReport(gradleBuild.getProjectDir())).contains(messages);
331351
}
332352
catch (UnexpectedBuildSuccess ex) {
@@ -371,7 +391,9 @@ private static final class GradleBuild {
371391

372392
private final Set<String> dependencies = new LinkedHashSet<>();
373393

374-
private final Map<Task, Boolean> prohibitObjectsRequireNonNull = new LinkedHashMap<>();
394+
private String conditionalOnClassAnnotation;
395+
396+
private Boolean prohibitObjectsRequireNonNull;
375397

376398
private GradleBuild(Path projectDir) {
377399
this.projectDir = projectDir;
@@ -381,8 +403,13 @@ Path getProjectDir() {
381403
return this.projectDir;
382404
}
383405

384-
GradleBuild withProhibitObjectsRequireNonNull(Task task, boolean prohibitObjectsRequireNonNull) {
385-
this.prohibitObjectsRequireNonNull.put(task, prohibitObjectsRequireNonNull);
406+
GradleBuild withConditionalOnClassAnnotation(String annotationName) {
407+
this.conditionalOnClassAnnotation = annotationName;
408+
return this;
409+
}
410+
411+
GradleBuild withProhibitObjectsRequireNonNull(Boolean prohibitObjectsRequireNonNull) {
412+
this.prohibitObjectsRequireNonNull = prohibitObjectsRequireNonNull;
386413
return this;
387414
}
388415

@@ -417,13 +444,20 @@ private GradleRunner prepareRunner(String... arguments) throws IOException {
417444
for (String dependency : this.dependencies) {
418445
buildFile.append(" implementation '%s'\n".formatted(dependency));
419446
}
420-
buildFile.append("}\n");
447+
buildFile.append("}\n\n");
448+
}
449+
for (Task task : Task.values()) {
450+
buildFile.append(task).append(" {");
451+
if (this.conditionalOnClassAnnotation != null) {
452+
buildFile.append("\n conditionalOnClassAnnotationName = ")
453+
.append(StringUtils.quote(this.conditionalOnClassAnnotation));
454+
}
455+
if (this.prohibitObjectsRequireNonNull != null) {
456+
buildFile.append("\n prohibitObjectsRequireNonNull = ")
457+
.append(this.prohibitObjectsRequireNonNull);
458+
}
459+
buildFile.append("\n}\n\n");
421460
}
422-
this.prohibitObjectsRequireNonNull.forEach((task, prohibitObjectsRequireNonNull) -> buildFile.append(task)
423-
.append(" {\n")
424-
.append(" prohibitObjectsRequireNonNull = ")
425-
.append(prohibitObjectsRequireNonNull)
426-
.append("\n}\n\n"));
427461
Files.writeString(this.projectDir.resolve("build.gradle"), buildFile, StandardCharsets.UTF_8);
428462
return GradleRunner.create()
429463
.withProjectDir(this.projectDir.toFile())
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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+
@Target({ ElementType.TYPE, ElementType.METHOD })
28+
@Retention(RetentionPolicy.RUNTIME)
29+
public @interface ArchitectureCheckConditionalOnClass {
30+
31+
Class<?>[] value() default {};
32+
33+
String[] name() default {};
34+
35+
}
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.ArchitectureCheckConditionalOnClass;
20+
import org.springframework.context.annotation.Bean;
21+
22+
class OnBeanMethod {
23+
24+
@Bean
25+
@ArchitectureCheckConditionalOnClass(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 & 9 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,27 @@ public AuditListener auditListener(AuditEventRepository auditEventRepository) {
5051
return new AuditListener(auditEventRepository);
5152
}
5253

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

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

6777
}

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)