Align AOT contributions for injection with the runtime behavior

Bean post processors that use InjectionMetadata checks if a property
value for the element it is about to inject is set and skip it, so
that the property value is used. Previously, the AOT contribution for
the same behavior did not check if a matching property value is set
and therefore override the user-defined value.

This commit introduces an additional method that filters the injected
element list so that only the elements that should be processed are
defined.

Closes gh-30476
This commit is contained in:
Stephane Nicoll
2023-05-11 16:00:21 +02:00
parent a9b94241af
commit c3c5eaf914
5 changed files with 79 additions and 14 deletions

View File

@@ -359,7 +359,8 @@ public class PersistenceAnnotationBeanPostProcessor implements InstantiationAwar
String beanName = registeredBean.getBeanName();
RootBeanDefinition beanDefinition = registeredBean.getMergedBeanDefinition();
InjectionMetadata metadata = findInjectionMetadata(beanDefinition, beanClass, beanName);
Collection<InjectedElement> injectedElements = metadata.getInjectedElements();
Collection<InjectedElement> injectedElements = metadata.getInjectedElements(
registeredBean.getMergedBeanDefinition().getPropertyValues());
if (!CollectionUtils.isEmpty(injectedElements)) {
return new AotContribution(beanClass, injectedElements);
}

View File

@@ -43,6 +43,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
import org.springframework.core.test.tools.CompileWithForkedClassLoader;
import org.springframework.core.test.tools.Compiled;
import org.springframework.core.test.tools.TestCompiler;
import org.springframework.lang.Nullable;
import org.springframework.util.ReflectionUtils;
import static org.assertj.core.api.Assertions.assertThat;
@@ -67,6 +68,20 @@ class PersistenceAnnotationBeanPostProcessorAotContributionTests {
this.generationContext = new TestGenerationContext();
}
@Test
void processAheadOfTimeWhenPersistenceUnitOnFieldAndPropertyValueSet() {
RegisteredBean registeredBean = registerBean(DefaultPersistenceUnitField.class);
registeredBean.getMergedBeanDefinition().getPropertyValues().add("emf", "myEntityManagerFactory");
assertThat(processAheadOfTime(registeredBean)).isNotNull(); // Field not handled by property values
}
@Test
void processAheadOfTimeWhenPersistenceUnitOnMethodAndPropertyValueSet() {
RegisteredBean registeredBean = registerBean(DefaultPersistenceUnitMethod.class);
registeredBean.getMergedBeanDefinition().getPropertyValues().add("emf", "myEntityManagerFactory");
assertThat(processAheadOfTime(registeredBean)).isNull();
}
@Test
void processAheadOfTimeWhenPersistenceUnitOnPublicField() {
RegisteredBean registeredBean = registerBean(DefaultPersistenceUnitField.class);
@@ -192,9 +207,7 @@ class PersistenceAnnotationBeanPostProcessorAotContributionTests {
private void testCompile(RegisteredBean registeredBean,
BiConsumer<BiConsumer<RegisteredBean, Object>, Compiled> result) {
PersistenceAnnotationBeanPostProcessor postProcessor = new PersistenceAnnotationBeanPostProcessor();
BeanRegistrationAotContribution contribution = postProcessor
.processAheadOfTime(registeredBean);
BeanRegistrationAotContribution contribution = processAheadOfTime(registeredBean);
BeanRegistrationCode beanRegistrationCode = mock();
contribution.applyTo(generationContext, beanRegistrationCode);
generationContext.writeGeneratedContent();
@@ -202,6 +215,12 @@ class PersistenceAnnotationBeanPostProcessorAotContributionTests {
.compile(compiled -> result.accept(new Invoker(compiled), compiled));
}
@Nullable
private BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
PersistenceAnnotationBeanPostProcessor postProcessor = new PersistenceAnnotationBeanPostProcessor();
return postProcessor.processAheadOfTime(registeredBean);
}
static class Invoker implements BiConsumer<RegisteredBean, Object> {
private Compiled compiled;