Skip to content

Commit 4f0d75b

Browse files
committed
Adapt to AOT Infrastructure changes in Commons.
See spring-projects/spring-data-commons#3267
1 parent 01696e5 commit 4f0d75b

File tree

10 files changed

+91
-166
lines changed

10 files changed

+91
-166
lines changed

spring-data-mongodb/src/jmh/java/org/springframework/data/mongodb/repository/AotRepositoryBenchmark.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.openjdk.jmh.annotations.TearDown;
2525

2626
import org.springframework.aot.test.generate.TestGenerationContext;
27+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
2728
import org.springframework.core.test.tools.TestCompiler;
2829
import org.springframework.data.mongodb.core.MongoOperations;
2930
import org.springframework.data.mongodb.core.MongoTemplate;
@@ -57,6 +58,7 @@ public static class BenchmarkParameters {
5758

5859
public static Class<?> aot;
5960
public static TestMongoAotRepositoryContext repositoryContext = new TestMongoAotRepositoryContext(
61+
new DefaultListableBeanFactory(),
6062
SmallerPersonRepository.class,
6163
RepositoryComposition.of(RepositoryFragment.structural(SimpleMongoRepository.class),
6264
RepositoryFragment.structural(QuerydslMongoPredicateExecutor.class)));

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/aot/MongoAotPredicates.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
public class MongoAotPredicates {
3535

3636
public static final Predicate<Class<?>> IS_SIMPLE_TYPE = (type) -> MongoSimpleTypes.HOLDER.isSimpleType(type)
37-
|| TypeUtils.type(type).isPartOf("org.bson");
37+
|| TypeUtils.type(type).isPartOf("org.bson") || TypeUtils.type(type).isPartOf("com.mongodb");
3838
public static final Predicate<ReactiveLibrary> IS_REACTIVE_LIBARARY_AVAILABLE = ReactiveWrappers::isAvailable;
3939
public static final Predicate<ClassLoader> IS_SYNC_CLIENT_PRESENT = (classLoader) -> ClassUtils
4040
.isPresent("com.mongodb.client.MongoClient", classLoader);

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/aot/MongoManagedTypesBeanRegistrationAotProcessor.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,8 @@ protected boolean isMongoManagedTypes(@Nullable Class<?> beanType) {
4545
}
4646

4747
@Override
48-
protected void contributeType(ResolvableType type, GenerationContext generationContext, AotContext aotContext) {
49-
50-
if (MongoAotPredicates.IS_SIMPLE_TYPE.test(type.toClass())) {
51-
return;
52-
}
53-
54-
super.contributeType(type, generationContext, aotContext);
48+
protected void registerTypeHints(ResolvableType type, AotContext aotContext, GenerationContext generationContext) {
49+
super.registerTypeHints(type, aotContext, generationContext);
5550
lazyLoadingProxyAotProcessor.registerLazyLoadingProxyIfNeeded(type.toClass(), generationContext);
5651
}
5752
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
* Copyright 2025 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.mongodb.aot;
17+
18+
import java.lang.reflect.Field;
19+
import java.lang.reflect.Method;
20+
import java.util.function.Predicate;
21+
22+
import org.springframework.data.util.Predicates;
23+
import org.springframework.data.util.TypeCollector;
24+
25+
/**
26+
* {@link TypeCollector} predicates to exclude MongoDB driver types.
27+
*
28+
* @author Mark Paluch
29+
* @since 5.0
30+
*/
31+
class MongoTypeFilters implements TypeCollector.TypeCollectorFilters {
32+
33+
@Override
34+
public Predicate<Class<?>> classPredicate() {
35+
return MongoAotPredicates.IS_SIMPLE_TYPE.negate();
36+
}
37+
38+
@Override
39+
public Predicate<Field> fieldPredicate() {
40+
return Predicates.<Field> declaringClass(MongoAotPredicates.IS_SIMPLE_TYPE).negate();
41+
}
42+
43+
@Override
44+
public Predicate<Method> methodPredicate() {
45+
return Predicates.<Method> declaringClass(MongoAotPredicates.IS_SIMPLE_TYPE).negate();
46+
}
47+
48+
}

spring-data-mongodb/src/main/java/org/springframework/data/mongodb/repository/aot/AotMongoRepositoryPostProcessor.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,12 @@
2323
import org.springframework.data.repository.config.AotRepositoryContext;
2424
import org.springframework.data.repository.config.RepositoryRegistrationAotProcessor;
2525
import org.springframework.data.util.TypeContributor;
26-
import org.springframework.data.util.TypeUtils;
2726

2827
/**
2928
* Mongodb-specific {@link RepositoryRegistrationAotProcessor} that contributes generated code for repositories.
3029
*
3130
* @author Christoph Strobl
31+
* @author Mark Paluch
3232
* @since 4.0
3333
*/
3434
public class AotMongoRepositoryPostProcessor extends RepositoryRegistrationAotProcessor {
@@ -38,32 +38,24 @@ public class AotMongoRepositoryPostProcessor extends RepositoryRegistrationAotPr
3838
private final LazyLoadingProxyAotProcessor lazyLoadingProxyAotProcessor = new LazyLoadingProxyAotProcessor();
3939

4040
@Override
41-
protected @Nullable MongoRepositoryContributor contribute(AotRepositoryContext repositoryContext,
41+
protected void configureTypeContributions(AotRepositoryContext repositoryContext,
4242
GenerationContext generationContext) {
43-
44-
// do some custom type registration here
45-
super.contribute(repositoryContext, generationContext);
43+
super.configureTypeContributions(repositoryContext, generationContext);
4644

4745
repositoryContext.getResolvedTypes().stream().filter(MongoAotPredicates.IS_SIMPLE_TYPE.negate()).forEach(type -> {
4846
TypeContributor.contribute(type, it -> true, generationContext);
4947
lazyLoadingProxyAotProcessor.registerLazyLoadingProxyIfNeeded(type, generationContext);
5048
});
51-
52-
if (!repositoryContext.isGeneratedRepositoriesEnabled(MODULE_NAME)) {
53-
return null;
54-
}
55-
56-
return new MongoRepositoryContributor(repositoryContext);
5749
}
5850

5951
@Override
60-
protected void contributeType(Class<?> type, GenerationContext generationContext) {
52+
protected @Nullable MongoRepositoryContributor contributeAotRepository(AotRepositoryContext repositoryContext) {
6153

62-
if (TypeUtils.type(type).isPartOf("org.springframework.data.mongodb", "com.mongodb")) {
63-
return;
54+
if (!repositoryContext.isGeneratedRepositoriesEnabled(MODULE_NAME)) {
55+
return null;
6456
}
6557

66-
super.contributeType(type, generationContext);
58+
return new MongoRepositoryContributor(repositoryContext);
6759
}
6860

6961
}

spring-data-mongodb/src/main/resources/META-INF/spring/aot.factories

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,3 +4,6 @@ org.springframework.aot.hint.RuntimeHintsRegistrar=\
44

55
org.springframework.beans.factory.aot.BeanRegistrationAotProcessor=\
66
org.springframework.data.mongodb.aot.MongoManagedTypesBeanRegistrationAotProcessor
7+
8+
org.springframework.data.util.TypeCollector$TypeCollectorFilters=\
9+
org.springframework.data.mongodb.aot.MongoTypeFilters

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/aot/AotFragmentTestConfigurationSupport.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,6 @@ public class AotFragmentTestConfigurationSupport implements BeanFactoryPostProce
5454

5555
private final Class<?> repositoryInterface;
5656
private final boolean registerFragmentFacade;
57-
private final TestMongoAotRepositoryContext repositoryContext;
5857

5958
public AotFragmentTestConfigurationSupport(Class<?> repositoryInterface) {
6059
this(repositoryInterface, true);
@@ -64,15 +63,15 @@ public AotFragmentTestConfigurationSupport(Class<?> repositoryInterface, boolean
6463

6564
this.repositoryInterface = repositoryInterface;
6665
this.registerFragmentFacade = registerFragmentFacade;
67-
this.repositoryContext = new TestMongoAotRepositoryContext(repositoryInterface, null);
6866
}
6967

7068
@Override
7169
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
7270

71+
TestMongoAotRepositoryContext repositoryContext = new TestMongoAotRepositoryContext(beanFactory,
72+
repositoryInterface, null);
7373
TestGenerationContext generationContext = new TestGenerationContext(repositoryInterface);
7474

75-
repositoryContext.setBeanFactory(beanFactory);
7675
new MongoRepositoryContributor(repositoryContext).contribute(generationContext);
7776
generationContext.writeGeneratedContent();
7877

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/aot/AotMongoRepositoryPostProcessorUnitTests.java

Lines changed: 16 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,11 @@
1919
import static org.mockito.Mockito.*;
2020

2121
import java.lang.annotation.Annotation;
22-
import java.util.Collection;
2322
import java.util.Collections;
2423
import java.util.List;
2524
import java.util.Set;
26-
import java.util.function.Consumer;
2725

2826
import org.jmolecules.ddd.annotation.Entity;
29-
import org.jspecify.annotations.Nullable;
3027
import org.junit.jupiter.api.Test;
3128
import org.junitpioneer.jupiter.ClearSystemProperty;
3229
import org.junitpioneer.jupiter.SetSystemProperty;
@@ -36,18 +33,14 @@
3633
import org.springframework.aot.generate.DefaultGenerationContext;
3734
import org.springframework.aot.generate.GenerationContext;
3835
import org.springframework.aot.generate.InMemoryGeneratedFiles;
39-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
4036
import org.springframework.context.support.AbstractApplicationContext;
4137
import org.springframework.context.support.GenericApplicationContext;
4238
import org.springframework.core.annotation.MergedAnnotation;
43-
import org.springframework.core.env.Environment;
44-
import org.springframework.core.env.StandardEnvironment;
4539
import org.springframework.data.annotation.Id;
4640
import org.springframework.data.aot.AotContext;
47-
import org.springframework.data.aot.AotTypeConfiguration;
4841
import org.springframework.data.mongodb.repository.support.SimpleMongoRepository;
4942
import org.springframework.data.repository.Repository;
50-
import org.springframework.data.repository.config.AotRepositoryContext;
43+
import org.springframework.data.repository.config.AotRepositoryContextSupport;
5144
import org.springframework.data.repository.config.AotRepositoryInformation;
5245
import org.springframework.data.repository.config.RepositoryConfigurationSource;
5346
import org.springframework.data.repository.core.RepositoryInformation;
@@ -65,10 +58,10 @@ class AotMongoRepositoryPostProcessorUnitTests {
6558
@SetSystemProperty(key = AotDetector.AOT_ENABLED, value = "true")
6659
void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() {
6760

68-
GenerationContext ctx = createGenerationContext();
6961
GenericApplicationContext context = new GenericApplicationContext();
62+
context.refresh();
7063

71-
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
64+
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context);
7265

7366
assertThat(contributor).isNotNull();
7467
}
@@ -77,10 +70,10 @@ void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() {
7770
@ClearSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED)
7871
void shouldEnableAotRepositoriesByDefault() {
7972

80-
GenerationContext ctx = createGenerationContext();
8173
GenericApplicationContext context = new GenericApplicationContext();
74+
context.refresh();
8275

83-
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
76+
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context);
8477

8578
assertThat(contributor).isNotNull();
8679
}
@@ -89,10 +82,10 @@ void shouldEnableAotRepositoriesByDefault() {
8982
@SetSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED, value = "false")
9083
void shouldDisableAotRepositoriesWhenGeneratedRepositoriesIsFalse() {
9184

92-
GenerationContext ctx = createGenerationContext();
9385
GenericApplicationContext context = new GenericApplicationContext();
86+
context.refresh();
9487

95-
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
88+
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context);
9689

9790
assertThat(contributor).isNull();
9891
}
@@ -101,10 +94,10 @@ void shouldDisableAotRepositoriesWhenGeneratedRepositoriesIsFalse() {
10194
@SetSystemProperty(key = "spring.aot.mongodb.repositories.enabled", value = "false")
10295
void shouldDisableAotRepositoriesWhenJpaGeneratedRepositoriesIsFalse() {
10396

104-
GenerationContext ctx = createGenerationContext();
10597
GenericApplicationContext context = new GenericApplicationContext();
98+
context.refresh();
10699

107-
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
100+
MongoRepositoryContributor contributor = createContributorWithPersonTypes(context);
108101

109102
assertThat(contributor).isNull();
110103
}
@@ -113,15 +106,14 @@ private GenerationContext createGenerationContext() {
113106
return new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT), new InMemoryGeneratedFiles());
114107
}
115108

116-
private MongoRepositoryContributor createContributorWithPersonTypes(GenericApplicationContext context,
117-
GenerationContext ctx) {
109+
private MongoRepositoryContributor createContributorWithPersonTypes(GenericApplicationContext context) {
118110

119-
return new AotMongoRepositoryPostProcessor().contribute(new DummyAotRepositoryContext(context) {
111+
return new AotMongoRepositoryPostProcessor().contributeAotRepository(new DummyAotRepositoryContext(context) {
120112
@Override
121113
public Set<Class<?>> getResolvedTypes() {
122114
return Collections.singleton(Person.class);
123115
}
124-
}, ctx);
116+
});
125117
}
126118

127119
@Entity
@@ -131,22 +123,15 @@ static class Person {
131123

132124
interface PersonRepository extends Repository<Person, Long> {}
133125

134-
static class DummyAotRepositoryContext implements AotRepositoryContext {
126+
static class DummyAotRepositoryContext extends AotRepositoryContextSupport {
135127

136-
private final @Nullable AbstractApplicationContext applicationContext;
137-
138-
DummyAotRepositoryContext(@Nullable AbstractApplicationContext applicationContext) {
139-
this.applicationContext = applicationContext;
140-
}
141-
142-
@Override
143-
public String getBeanName() {
144-
return "jpaRepository";
128+
DummyAotRepositoryContext(AbstractApplicationContext applicationContext) {
129+
super(AotContext.from(applicationContext, applicationContext.getEnvironment()));
145130
}
146131

147132
@Override
148133
public String getModuleName() {
149-
return "JPA";
134+
return "MongoDB";
150135
}
151136

152137
@Override
@@ -180,40 +165,6 @@ public Set<Class<?>> getResolvedTypes() {
180165
return Set.of();
181166
}
182167

183-
@Override
184-
public Set<Class<?>> getUserDomainTypes() {
185-
return Set.of();
186-
}
187-
188-
@Override
189-
public ConfigurableListableBeanFactory getBeanFactory() {
190-
return applicationContext != null ? applicationContext.getBeanFactory() : null;
191-
}
192-
193-
@Override
194-
public Environment getEnvironment() {
195-
return applicationContext == null ? new StandardEnvironment() : applicationContext.getEnvironment();
196-
}
197-
198-
@Override
199-
public TypeIntrospector introspectType(String typeName) {
200-
return null;
201-
}
202-
203-
@Override
204-
public IntrospectedBeanDefinition introspectBeanDefinition(String beanName) {
205-
return null;
206-
}
207-
208-
@Override
209-
public void typeConfiguration(Class<?> type, Consumer<AotTypeConfiguration> configurationConsumer) {
210-
211-
}
212-
213-
@Override
214-
public Collection<AotTypeConfiguration> typeConfigurations() {
215-
return List.of();
216-
}
217168
}
218169

219170
}

spring-data-mongodb/src/test/java/org/springframework/data/mongodb/repository/aot/QueryMethodContributionUnitTests.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.junit.jupiter.api.Assertions;
3030
import org.junit.jupiter.api.Test;
3131

32+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
3233
import org.springframework.data.domain.Limit;
3334
import org.springframework.data.domain.Pageable;
3435
import org.springframework.data.domain.Range;
@@ -391,7 +392,8 @@ private static MethodSpec codeOf(Class<?> repository, String methodName, Class<?
391392

392393
Method method = repository.getMethod(methodName, args);
393394

394-
TestMongoAotRepositoryContext repoContext = new TestMongoAotRepositoryContext(repository, null);
395+
TestMongoAotRepositoryContext repoContext = new TestMongoAotRepositoryContext(new DefaultListableBeanFactory(),
396+
repository, null);
395397
MongoRepositoryContributor contributor = new MongoRepositoryContributor(repoContext);
396398
MethodContributor<? extends QueryMethod> methodContributor = contributor.contributeQueryMethod(method);
397399

0 commit comments

Comments
 (0)