Allow infrastructure beans to be injected into BPP @Bean methods

Closes gh-43928
This commit is contained in:
Andy Wilkinson
2025-01-22 14:34:41 +00:00
parent 88ce6f6af2
commit e7eca56dde

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 the original author or authors.
* Copyright 2012-2025 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -69,6 +69,8 @@ import org.gradle.api.tasks.PathSensitivity;
import org.gradle.api.tasks.SkipWhenEmpty;
import org.gradle.api.tasks.TaskAction;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.context.annotation.Role;
import org.springframework.util.ResourceUtils;
/**
@@ -149,7 +151,8 @@ public abstract class ArchitectureCheck extends DefaultTask {
DescribedPredicate<JavaClass> notOfASafeType = DescribedPredicate
.not(Predicates.assignableTo("org.springframework.beans.factory.ObjectProvider")
.or(Predicates.assignableTo("org.springframework.context.ApplicationContext"))
.or(Predicates.assignableTo("org.springframework.core.env.Environment")));
.or(Predicates.assignableTo("org.springframework.core.env.Environment")
.or(annotatedWithRoleInfrastructure())));
return new ArchCondition<>("not have parameters that will cause eager initialization") {
@Override
@@ -167,6 +170,18 @@ public abstract class ArchitectureCheck extends DefaultTask {
};
}
private DescribedPredicate<JavaClass> annotatedWithRoleInfrastructure() {
return new DescribedPredicate<>("annotated with @Role(BeanDefinition.ROLE_INFRASTRUCTURE") {
@Override
public boolean test(JavaClass candidate) {
Role role = candidate.getAnnotationOfType(Role.class);
return (role != null) && (role.value() == BeanDefinition.ROLE_INFRASTRUCTURE);
}
};
}
private ArchRule allBeanFactoryPostProcessorBeanMethodsShouldBeStaticAndHaveNoParameters() {
return ArchRuleDefinition.methods()
.that()