Skip to content

Commit e7eca56

Browse files
committed
Allow infrastructure beans to be injected into BPP @bean methods
Closes gh-43928
1 parent 88ce6f6 commit e7eca56

File tree

1 file changed

+17
-2
lines changed

1 file changed

+17
-2
lines changed

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

+17-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2024 the original author or authors.
2+
* Copyright 2012-2025 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.
@@ -69,6 +69,8 @@
6969
import org.gradle.api.tasks.SkipWhenEmpty;
7070
import org.gradle.api.tasks.TaskAction;
7171

72+
import org.springframework.beans.factory.config.BeanDefinition;
73+
import org.springframework.context.annotation.Role;
7274
import org.springframework.util.ResourceUtils;
7375

7476
/**
@@ -149,7 +151,8 @@ private ArchCondition<JavaMethod> onlyHaveParametersThatWillNotCauseEagerInitial
149151
DescribedPredicate<JavaClass> notOfASafeType = DescribedPredicate
150152
.not(Predicates.assignableTo("org.springframework.beans.factory.ObjectProvider")
151153
.or(Predicates.assignableTo("org.springframework.context.ApplicationContext"))
152-
.or(Predicates.assignableTo("org.springframework.core.env.Environment")));
154+
.or(Predicates.assignableTo("org.springframework.core.env.Environment")
155+
.or(annotatedWithRoleInfrastructure())));
153156
return new ArchCondition<>("not have parameters that will cause eager initialization") {
154157

155158
@Override
@@ -167,6 +170,18 @@ public void check(JavaMethod item, ConditionEvents events) {
167170
};
168171
}
169172

173+
private DescribedPredicate<JavaClass> annotatedWithRoleInfrastructure() {
174+
return new DescribedPredicate<>("annotated with @Role(BeanDefinition.ROLE_INFRASTRUCTURE") {
175+
176+
@Override
177+
public boolean test(JavaClass candidate) {
178+
Role role = candidate.getAnnotationOfType(Role.class);
179+
return (role != null) && (role.value() == BeanDefinition.ROLE_INFRASTRUCTURE);
180+
}
181+
182+
};
183+
}
184+
170185
private ArchRule allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters() {
171186
return ArchRuleDefinition.methods()
172187
.that()

0 commit comments

Comments
 (0)