Skip to content

Commit b8c4702

Browse files
committed
Adapt to AOT API changes in the core Spring Framework.
This minimally gets the Spring Data codebase with AOT processing support compiling again.
1 parent 7703e94 commit b8c4702

15 files changed

+497
-446
lines changed

src/main/java/org/springframework/data/aot/AotContributingRepositoryBeanPostProcessor.java

-189
This file was deleted.

src/main/java/org/springframework/data/aot/AotDataComponentsBeanFactoryPostProcessor.java renamed to src/main/java/org/springframework/data/aot/AotDataComponentsBeanFactoryInitializationAotProcessor.java

+19-9
Original file line numberDiff line numberDiff line change
@@ -19,40 +19,50 @@
1919

2020
import org.apache.commons.logging.Log;
2121
import org.apache.commons.logging.LogFactory;
22+
23+
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution;
24+
import org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor;
2225
import org.springframework.beans.factory.config.BeanDefinition;
2326
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
2427
import org.springframework.beans.factory.config.ConstructorArgumentValues.ValueHolder;
25-
import org.springframework.beans.factory.generator.AotContributingBeanFactoryPostProcessor;
26-
import org.springframework.beans.factory.generator.BeanFactoryContribution;
2728
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
2829
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
2930
import org.springframework.data.ManagedTypes;
31+
import org.springframework.lang.NonNull;
3032
import org.springframework.lang.Nullable;
3133

3234
/**
33-
* {@link AotContributingBeanFactoryPostProcessor} implementation to capture common data infrastructure concerns.
35+
* {@link BeanFactoryInitializationAotProcessor} implementation used to encapsulate common data infrastructure concerns.
3436
*
3537
* @author Christoph Strobl
38+
* @author John Blum
39+
* @see org.springframework.beans.factory.aot.BeanFactoryInitializationAotContribution
40+
* @see org.springframework.beans.factory.aot.BeanFactoryInitializationAotProcessor
3641
* @since 3.0
3742
*/
38-
public class AotDataComponentsBeanFactoryPostProcessor implements AotContributingBeanFactoryPostProcessor {
43+
public class AotDataComponentsBeanFactoryInitializationAotProcessor implements BeanFactoryInitializationAotProcessor {
3944

40-
private static final Log logger = LogFactory.getLog(AotContributingBeanFactoryPostProcessor.class);
45+
private static final Log logger = LogFactory.getLog(BeanFactoryInitializationAotProcessor.class);
4146

4247
@Nullable
4348
@Override
44-
public BeanFactoryContribution contribute(ConfigurableListableBeanFactory beanFactory) {
45-
postProcessManagedTypes(beanFactory);
49+
public BeanFactoryInitializationAotContribution processAheadOfTime(
50+
@NonNull ConfigurableListableBeanFactory beanFactory) {
51+
52+
processManagedTypes(beanFactory);
4653
return null;
4754
}
4855

49-
private void postProcessManagedTypes(ConfigurableListableBeanFactory beanFactory) {
56+
private void processManagedTypes(@NonNull ConfigurableListableBeanFactory beanFactory) {
5057

5158
if (beanFactory instanceof BeanDefinitionRegistry registry) {
5259
for (String beanName : beanFactory.getBeanNamesForType(ManagedTypes.class)) {
5360

5461
BeanDefinition beanDefinition = beanFactory.getBeanDefinition(beanName);
55-
ValueHolder argumentValue = beanDefinition.getConstructorArgumentValues().getArgumentValue(0, null, null, null);
62+
63+
ValueHolder argumentValue = beanDefinition.getConstructorArgumentValues()
64+
.getArgumentValue(0, null, null, null);
65+
5666
if (argumentValue.getValue() instanceof Supplier supplier) {
5767

5868
if (logger.isDebugEnabled()) {

src/main/java/org/springframework/data/aot/AotManagedTypesPostProcessor.java

+28-26
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,14 @@
2121

2222
import org.apache.commons.logging.Log;
2323
import org.apache.commons.logging.LogFactory;
24-
25-
import org.springframework.aot.generator.CodeContribution;
24+
import org.springframework.aot.generate.GenerationContext;
2625
import org.springframework.beans.BeansException;
2726
import org.springframework.beans.factory.BeanFactory;
2827
import org.springframework.beans.factory.BeanFactoryAware;
29-
import org.springframework.beans.factory.generator.AotContributingBeanPostProcessor;
30-
import org.springframework.beans.factory.generator.BeanInstantiationContribution;
28+
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
29+
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
30+
import org.springframework.beans.factory.aot.BeanRegistrationCode;
31+
import org.springframework.beans.factory.support.RegisteredBean;
3132
import org.springframework.beans.factory.support.RootBeanDefinition;
3233
import org.springframework.core.ResolvableType;
3334
import org.springframework.data.ManagedTypes;
@@ -37,15 +38,16 @@
3738
import org.springframework.util.StringUtils;
3839

3940
/**
40-
* {@link AotContributingBeanPostProcessor} handling {@link #getModulePrefix() prefixed} {@link ManagedTypes} instances.
41+
* {@link BeanRegistrationAotProcessor} handling {@link #getModulePrefix() prefixed} {@link ManagedTypes} instances.
4142
* This allows to register store specific handling of discovered types.
4243
*
4344
* @author Christoph Strobl
45+
* @author John Blum
4446
* @see org.springframework.beans.factory.BeanFactoryAware
45-
* @see org.springframework.beans.factory.generator.AotContributingBeanPostProcessor
47+
* @see org.springframework.beans.factory.aot.BeanRegistrationAotProcessor
4648
* @since 3.0
4749
*/
48-
public class AotManagedTypesPostProcessor implements AotContributingBeanPostProcessor, BeanFactoryAware {
50+
public class AotManagedTypesPostProcessor implements BeanRegistrationAotProcessor, BeanFactoryAware {
4951

5052
private static final Log logger = LogFactory.getLog(AotManagedTypesPostProcessor.class);
5153

@@ -54,9 +56,13 @@ public class AotManagedTypesPostProcessor implements AotContributingBeanPostProc
5456
@Nullable
5557
private String modulePrefix;
5658

57-
@Nullable
5859
@Override
59-
public BeanInstantiationContribution contribute(@NonNull RootBeanDefinition beanDefinition,
60+
public BeanRegistrationAotContribution processAheadOfTime(@NonNull RegisteredBean registeredBean) {
61+
return contribute(registeredBean.getMergedBeanDefinition(), registeredBean.getBeanClass(), registeredBean.getBeanName());
62+
}
63+
64+
@Nullable
65+
protected BeanRegistrationAotContribution contribute(@NonNull RootBeanDefinition beanDefinition,
6066
@NonNull Class<?> beanType, @NonNull String beanName) {
6167

6268
return isMatch(beanType, beanName)
@@ -77,33 +83,33 @@ protected boolean matchesPrefix(@Nullable String beanName) {
7783
}
7884

7985
/**
80-
* Hook to provide a customized flavor of {@link BeanInstantiationContribution}. By overriding this method calls to
81-
* {@link #contributeType(ResolvableType, CodeContribution)} might no longer be issued.
86+
* Hook to provide a customized flavor of {@link BeanRegistrationAotContribution}. By overriding this method
87+
* calls to {@link #contributeType(ResolvableType, GenerationContext)} might no longer be issued.
8288
*
8389
* @param aotContext never {@literal null}.
8490
* @param managedTypes never {@literal null}.
8591
* @return new instance of {@link AotManagedTypesPostProcessor} or {@literal null} if nothing to do.
8692
*/
8793
@Nullable
88-
protected BeanInstantiationContribution contribute(AotContext aotContext, ManagedTypes managedTypes) {
94+
protected BeanRegistrationAotContribution contribute(AotContext aotContext, ManagedTypes managedTypes) {
8995
return new ManagedTypesContribution(aotContext, managedTypes, this::contributeType);
9096
}
9197

9298
/**
9399
* Hook to contribute configuration for a given {@literal type}.
94100
*
95101
* @param type never {@literal null}.
96-
* @param contribution never {@literal null}.
102+
* @param generationContext never {@literal null}.
97103
*/
98-
protected void contributeType(ResolvableType type, CodeContribution contribution) {
104+
protected void contributeType(ResolvableType type, GenerationContext generationContext) {
99105

100106
if (logger.isDebugEnabled()) {
101107
logger.debug(String.format("Contributing type information for [%s].", type.getType()));
102108
}
103109

104-
TypeContributor.contribute(type.toClass(), Collections.singleton(TypeContributor.DATA_NAMESPACE), contribution);
110+
TypeContributor.contribute(type.toClass(), Collections.singleton(TypeContributor.DATA_NAMESPACE), generationContext);
105111
TypeUtils.resolveUsedAnnotations(type.toClass()).forEach(annotation -> TypeContributor
106-
.contribute(annotation.getType(), Collections.singleton(TypeContributor.DATA_NAMESPACE), contribution));
112+
.contribute(annotation.getType(), Collections.singleton(TypeContributor.DATA_NAMESPACE), generationContext));
107113
}
108114

109115
@Nullable
@@ -115,24 +121,19 @@ public void setModulePrefix(@Nullable String modulePrefix) {
115121
this.modulePrefix = modulePrefix;
116122
}
117123

118-
@Override
119-
public int getOrder() {
120-
return 0;
121-
}
122-
123124
@Override
124125
public void setBeanFactory(@NonNull BeanFactory beanFactory) throws BeansException {
125126
this.beanFactory = beanFactory;
126127
}
127128

128-
static class ManagedTypesContribution implements BeanInstantiationContribution {
129+
static class ManagedTypesContribution implements BeanRegistrationAotContribution {
129130

130131
private final AotContext aotContext;
131132
private final ManagedTypes managedTypes;
132-
private final BiConsumer<ResolvableType, CodeContribution> contributionAction;
133+
private final BiConsumer<ResolvableType, GenerationContext> contributionAction;
133134

134135
public ManagedTypesContribution(AotContext aotContext, ManagedTypes managedTypes,
135-
BiConsumer<ResolvableType, CodeContribution> contributionAction) {
136+
BiConsumer<ResolvableType, GenerationContext> contributionAction) {
136137

137138
this.aotContext = aotContext;
138139
this.managedTypes = managedTypes;
@@ -148,12 +149,13 @@ public ManagedTypes getManagedTypes() {
148149
}
149150

150151
@Override
151-
public void applyTo(@NonNull CodeContribution contribution) {
152+
public void applyTo(@NonNull GenerationContext generationContext,
153+
@NonNull BeanRegistrationCode beanRegistrationCode) {
152154

153155
List<Class<?>> types = getManagedTypes().toList();
154156

155157
if (!types.isEmpty()) {
156-
TypeCollector.inspect(types).forEach(type -> contributionAction.accept(type, contribution));
158+
TypeCollector.inspect(types).forEach(type -> contributionAction.accept(type, generationContext));
157159
}
158160
}
159161
}

0 commit comments

Comments
 (0)