Skip to content

Commit

Permalink
Allow infrastructure beans to be injected into BPP @bean methods
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Jan 22, 2025
1 parent 88ce6f6 commit e7eca56
Showing 1 changed file with 17 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -69,6 +69,8 @@
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;

/**
Expand Down Expand Up @@ -149,7 +151,8 @@ private ArchCondition<JavaMethod> onlyHaveParametersThatWillNotCauseEagerInitial
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
Expand All @@ -167,6 +170,18 @@ public void check(JavaMethod item, ConditionEvents events) {
};
}

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()
Expand Down

0 comments on commit e7eca56

Please sign in to comment.