Skip to content

Commit

Permalink
Skip infrastructure beans
Browse files Browse the repository at this point in the history
  • Loading branch information
scordio committed Nov 24, 2024
1 parent a4af503 commit b060d6c
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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;
Expand Down Expand Up @@ -98,7 +99,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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,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;

Expand All @@ -56,6 +56,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}.
Expand Down Expand Up @@ -143,17 +146,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()));
}

Expand Down

0 comments on commit b060d6c

Please sign in to comment.