Skip to content

Commit e741d6e

Browse files
committed
Exclude bean definition from AOT processing using an attribute
This commits allows a particular bean definition to be excluded from AOT processing using an attribute. If BeanRegistrationAotProcessor#IGNORE_REGISTRATION_ATTRIBUTE is set to `true`, then the bean definition is excluded. This complement the existing BeanRegistrationExcludeFilter capability. Closes gh-33243
1 parent bbfc336 commit e741d6e

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorFactory.java

+4
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ private boolean isExcluded(RegisteredBean registeredBean) {
133133
}
134134

135135
private boolean isImplicitlyExcluded(RegisteredBean registeredBean) {
136+
if (Boolean.TRUE.equals(registeredBean.getMergedBeanDefinition()
137+
.getAttribute(BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE))) {
138+
return true;
139+
}
136140
Class<?> beanClass = registeredBean.getBeanClass();
137141
if (BeanFactoryInitializationAotProcessor.class.isAssignableFrom(beanClass)) {
138142
return true;

spring-beans/src/main/java/org/springframework/beans/factory/aot/BeanRegistrationAotProcessor.java

+9
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,15 @@
4949
@FunctionalInterface
5050
public interface BeanRegistrationAotProcessor {
5151

52+
/**
53+
* The name of an attribute that can be
54+
* {@link org.springframework.core.AttributeAccessor#setAttribute set} on a
55+
* {@link org.springframework.beans.factory.config.BeanDefinition} to signal
56+
* that its registration should not be processed.
57+
* @since 6.2
58+
*/
59+
String IGNORE_REGISTRATION_ATTRIBUTE = "aotProcessingIgnoreRegistration";
60+
5261
/**
5362
* Process the given {@link RegisteredBean} instance ahead-of-time and
5463
* return a contribution or {@code null}.

spring-beans/src/test/java/org/springframework/beans/factory/aot/BeanDefinitionMethodGeneratorFactoryTests.java

+35
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
* Tests for {@link BeanDefinitionMethodGeneratorFactory}.
3737
*
3838
* @author Phillip Webb
39+
* @author Stephane Nicoll
3940
*/
4041
class BeanDefinitionMethodGeneratorFactoryTests {
4142

@@ -58,6 +59,40 @@ void createWhenBeanRegistrationExcludeFilterFactoryIsNotAotProcessorLoads() {
5859
AotServices.factories(loader)));
5960
}
6061

62+
@Test
63+
void getBeanDefinitionMethodGeneratorWhenExcludedByBeanDefinitionAttributeReturnsNull() {
64+
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();
65+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
66+
RegisteredBean registeredBean = registerTestBean(beanFactory);
67+
registeredBean.getMergedBeanDefinition().setAttribute(
68+
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, true);
69+
BeanDefinitionMethodGeneratorFactory methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
70+
AotServices.factoriesAndBeans(springFactoriesLoader, beanFactory));
71+
assertThat(methodGeneratorFactory.getBeanDefinitionMethodGenerator(registeredBean)).isNull();
72+
}
73+
74+
@Test
75+
void getBeanDefinitionMethodGeneratorWhenBeanDefinitionAttributeSetToFalseDoesNotFilterBean() {
76+
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();
77+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
78+
RegisteredBean registeredBean = registerTestBean(beanFactory);
79+
registeredBean.getMergedBeanDefinition().setAttribute(
80+
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, false);
81+
BeanDefinitionMethodGeneratorFactory methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
82+
AotServices.factoriesAndBeans(springFactoriesLoader, beanFactory));
83+
assertThat(methodGeneratorFactory.getBeanDefinitionMethodGenerator(registeredBean)).isNotNull();
84+
}
85+
86+
@Test
87+
void getBeanDefinitionMethodGeneratorWhenBeanDefinitionAttributeIsNotSetDoesNotFilterBean() {
88+
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();
89+
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
90+
RegisteredBean registeredBean = registerTestBean(beanFactory);
91+
BeanDefinitionMethodGeneratorFactory methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
92+
AotServices.factoriesAndBeans(springFactoriesLoader, beanFactory));
93+
assertThat(methodGeneratorFactory.getBeanDefinitionMethodGenerator(registeredBean)).isNotNull();
94+
}
95+
6196
@Test
6297
void getBeanDefinitionMethodGeneratorWhenExcludedByBeanRegistrationExcludeFilterReturnsNull() {
6398
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();

0 commit comments

Comments
 (0)