From 5a50fcb175e63659b1d6ecc4233cf40871c12a85 Mon Sep 17 00:00:00 2001 From: Stefano Cordio Date: Sat, 23 Nov 2024 20:25:24 +0100 Subject: [PATCH] Skip infrastructure beans --- ...alidationBeanRegistrationAotProcessor.java | 3 ++- ...tionBeanRegistrationAotProcessorTests.java | 22 +++++++++++++++---- 2 files changed, 20 insertions(+), 5 deletions(-) diff --git a/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java b/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java index cae1c9a88c6..b265b46d588 100644 --- a/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java +++ b/spring-context/src/main/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessor.java @@ -44,6 +44,7 @@ import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor; import org.springframework.beans.factory.aot.BeanRegistrationCode; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.RegisteredBean; import org.springframework.core.KotlinDetector; import org.springframework.core.ResolvableType; @@ -99,7 +100,7 @@ private static Validator getValidatorIfAvailable() { @Nullable public static BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) { - if (validator == null) { + if (validator == null || BeanDefinition.ROLE_INFRASTRUCTURE == registeredBean.getMergedBeanDefinition().getRole()) { return null; } diff --git a/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java b/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java index 065dff57ec8..fe425f8bd57 100644 --- a/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java +++ b/spring-context/src/test/java/org/springframework/validation/beanvalidation/BeanValidationBeanRegistrationAotProcessorTests.java @@ -37,9 +37,9 @@ import org.springframework.aot.hint.predicate.RuntimeHintsPredicates; import org.springframework.aot.test.generate.TestGenerationContext; import org.springframework.beans.factory.aot.BeanRegistrationAotContribution; +import org.springframework.beans.factory.config.BeanDefinition; import org.springframework.beans.factory.support.DefaultListableBeanFactory; import org.springframework.beans.factory.support.RegisteredBean; -import org.springframework.beans.factory.support.RootBeanDefinition; import org.springframework.core.OverridingClassLoader; import org.springframework.lang.Nullable; @@ -52,6 +52,9 @@ import static java.lang.annotation.RetentionPolicy.RUNTIME; import static org.assertj.core.api.Assertions.assertThat; import static org.mockito.Mockito.mock; +import static org.springframework.beans.factory.config.BeanDefinition.ROLE_APPLICATION; +import static org.springframework.beans.factory.config.BeanDefinition.ROLE_INFRASTRUCTURE; +import static org.springframework.beans.factory.support.BeanDefinitionBuilder.rootBeanDefinition; /** * Tests for {@link BeanValidationBeanRegistrationAotProcessor}. @@ -130,17 +133,28 @@ void shouldSkipConstraintWithMissingDependency() throws Exception { assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).isEmpty(); } + @Test // gh-33940 + void shouldSkipInfrastructureBean() { + process(EmptyClass.class, ROLE_INFRASTRUCTURE); + assertThat(this.generationContext.getRuntimeHints().reflection().typeHints()).isEmpty(); + } + private void process(Class beanClass) { - BeanRegistrationAotContribution contribution = createContribution(beanClass); + process(beanClass, ROLE_APPLICATION); + } + + private void process(Class beanClass, int role) { + BeanRegistrationAotContribution contribution = createContribution(beanClass, role); if (contribution != null) { contribution.applyTo(this.generationContext, mock()); } } @Nullable - private BeanRegistrationAotContribution createContribution(Class beanClass) { + private BeanRegistrationAotContribution createContribution(Class beanClass, int role) { DefaultListableBeanFactory beanFactory = new DefaultListableBeanFactory(); - beanFactory.registerBeanDefinition(beanClass.getName(), new RootBeanDefinition(beanClass)); + BeanDefinition beanDefinition = rootBeanDefinition(beanClass).setRole(role).getBeanDefinition(); + beanFactory.registerBeanDefinition(beanClass.getName(), beanDefinition); return this.processor.processAheadOfTime(RegisteredBean.of(beanFactory, beanClass.getName())); }