From e7eca56dde49b42e88b835d9ce4c59d66e18551e Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Wed, 22 Jan 2025 14:34:41 +0000 Subject: [PATCH] Allow infrastructure beans to be injected into BPP @Bean methods Closes gh-43928 --- .../build/architecture/ArchitectureCheck.java | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java b/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java index d1d830d60e..6d97f85c50 100644 --- a/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java +++ b/buildSrc/src/main/java/org/springframework/boot/build/architecture/ArchitectureCheck.java @@ -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 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 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()