Skip to content

Commit

Permalink
Exclude bean definition from AOT processing using an attribute
Browse files Browse the repository at this point in the history
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 spring-projectsgh-33243
  • Loading branch information
snicoll committed Jul 21, 2024
1 parent e011d4f commit 4a39784
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,10 @@ private boolean isExcluded(RegisteredBean registeredBean) {
}

private boolean isImplicitlyExcluded(RegisteredBean registeredBean) {
if (Boolean.TRUE.equals(registeredBean.getMergedBeanDefinition()
.getAttribute(BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE))) {
return true;
}
Class<?> beanClass = registeredBean.getBeanClass();
if (BeanFactoryInitializationAotProcessor.class.isAssignableFrom(beanClass)) {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,15 @@
@FunctionalInterface
public interface BeanRegistrationAotProcessor {

/**
* The name of an attribute that can be
* {@link org.springframework.core.AttributeAccessor#setAttribute set} on a
* {@link org.springframework.beans.factory.config.BeanDefinition} to signal
* that its registration should not be processed.
* @since 6.2
*/
String IGNORE_REGISTRATION_ATTRIBUTE = "aotProcessingIgnoreRegistration";

/**
* Process the given {@link RegisteredBean} instance ahead-of-time and
* return a contribution or {@code null}.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
* Tests for {@link BeanDefinitionMethodGeneratorFactory}.
*
* @author Phillip Webb
* @author Stephane Nicoll
*/
class BeanDefinitionMethodGeneratorFactoryTests {

Expand All @@ -58,6 +59,40 @@ void createWhenBeanRegistrationExcludeFilterFactoryIsNotAotProcessorLoads() {
AotServices.factories(loader)));
}

@Test
void getBeanDefinitionMethodGeneratorWhenExcludedByBeanDefinitionAttributeReturnsNull() {
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RegisteredBean registeredBean = registerTestBean(beanFactory);
registeredBean.getMergedBeanDefinition().setAttribute(
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, true);
BeanDefinitionMethodGeneratorFactory methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
AotServices.factoriesAndBeans(springFactoriesLoader, beanFactory));
assertThat(methodGeneratorFactory.getBeanDefinitionMethodGenerator(registeredBean)).isNull();
}

@Test
void getBeanDefinitionMethodGeneratorWhenBeanDefinitionAttributeSetToFalseDoesNotFilterBean() {
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RegisteredBean registeredBean = registerTestBean(beanFactory);
registeredBean.getMergedBeanDefinition().setAttribute(
BeanRegistrationAotProcessor.IGNORE_REGISTRATION_ATTRIBUTE, false);
BeanDefinitionMethodGeneratorFactory methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
AotServices.factoriesAndBeans(springFactoriesLoader, beanFactory));
assertThat(methodGeneratorFactory.getBeanDefinitionMethodGenerator(registeredBean)).isNotNull();
}

@Test
void getBeanDefinitionMethodGeneratorWhenBeanDefinitionAttributeIsNotSetDoesNotFilterBean() {
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();
DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory();
RegisteredBean registeredBean = registerTestBean(beanFactory);
BeanDefinitionMethodGeneratorFactory methodGeneratorFactory = new BeanDefinitionMethodGeneratorFactory(
AotServices.factoriesAndBeans(springFactoriesLoader, beanFactory));
assertThat(methodGeneratorFactory.getBeanDefinitionMethodGenerator(registeredBean)).isNotNull();
}

@Test
void getBeanDefinitionMethodGeneratorWhenExcludedByBeanRegistrationExcludeFilterReturnsNull() {
MockSpringFactoriesLoader springFactoriesLoader = new MockSpringFactoriesLoader();
Expand Down

0 comments on commit 4a39784

Please sign in to comment.