Skip to content

Commit a5b5c2e

Browse files
committed
Adapt to AOT Infrastructure changes in Commons.
See spring-projects/spring-data-commons#3267
1 parent 03e9862 commit a5b5c2e

File tree

6 files changed

+61
-141
lines changed

6 files changed

+61
-141
lines changed

spring-data-jpa/src/jmh/java/org/springframework/data/jpa/benchmark/AotRepositoryQueryMethodBenchmarks.java

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import org.springframework.aot.test.generate.TestGenerationContext;
4141
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
4242
import org.springframework.beans.factory.support.DefaultBeanNameGenerator;
43+
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
4344
import org.springframework.core.env.StandardEnvironment;
4445
import org.springframework.core.io.DefaultResourceLoader;
4546
import org.springframework.core.test.tools.TestCompiler;
@@ -56,6 +57,7 @@
5657
import org.springframework.data.projection.ProjectionFactory;
5758
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
5859
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
60+
import org.springframework.data.repository.config.RepositoryConfigurationSource;
5961
import org.springframework.data.repository.core.RepositoryMetadata;
6062
import org.springframework.data.repository.core.support.RepositoryComposition;
6163
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
@@ -82,11 +84,16 @@ public class AotRepositoryQueryMethodBenchmarks {
8284
public static class BenchmarkParameters {
8385

8486
public static Class<?> aot;
85-
public static TestJpaAotRepositoryContext<PersonRepository> repositoryContext = new TestJpaAotRepositoryContext<>(
86-
PersonRepository.class, null,
87-
new AnnotationRepositoryConfigurationSource(AnnotationMetadata.introspect(SampleConfig.class),
88-
EnableJpaRepositories.class, new DefaultResourceLoader(), new StandardEnvironment(),
89-
Mockito.mock(BeanDefinitionRegistry.class), DefaultBeanNameGenerator.INSTANCE));
87+
public static TestJpaAotRepositoryContext<PersonRepository> repositoryContext;
88+
89+
static {
90+
RepositoryConfigurationSource configurationSource = new AnnotationRepositoryConfigurationSource(
91+
AnnotationMetadata.introspect(SampleConfig.class), EnableJpaRepositories.class, new DefaultResourceLoader(),
92+
new StandardEnvironment(), Mockito.mock(BeanDefinitionRegistry.class), DefaultBeanNameGenerator.INSTANCE);
93+
94+
repositoryContext = new TestJpaAotRepositoryContext<>(new DefaultListableBeanFactory(), PersonRepository.class,
95+
null, configurationSource);
96+
}
9097

9198
EntityManager entityManager;
9299
RepositoryComposition.RepositoryFragments fragments;

spring-data-jpa/src/main/java/org/springframework/data/jpa/repository/config/JpaRepositoryConfigExtension.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import org.springframework.core.io.ResourceLoader;
5959
import org.springframework.dao.DataAccessException;
6060
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
61+
import org.springframework.data.aot.AotContext;
6162
import org.springframework.data.jpa.repository.JpaRepository;
6263
import org.springframework.data.jpa.repository.aot.JpaRepositoryContributor;
6364
import org.springframework.data.jpa.repository.support.DefaultJpaContext;
@@ -374,10 +375,22 @@ static boolean isActive(@Nullable ClassLoader classLoader) {
374375
public static class JpaRepositoryRegistrationAotProcessor extends RepositoryRegistrationAotProcessor {
375376

376377
public static final String USE_ENTITY_MANAGER = "spring.aot.jpa.repositories.use-entitymanager";
378+
377379
private static final String MODULE_NAME = "jpa";
378380

379-
protected @Nullable JpaRepositoryContributor contribute(AotRepositoryContext repositoryContext,
381+
@Override
382+
protected void configureTypeContributions(AotRepositoryContext repositoryContext,
380383
GenerationContext generationContext) {
384+
super.configureTypeContributions(repositoryContext, generationContext);
385+
}
386+
387+
@Override
388+
protected void configureTypeContribution(Class<?> type, AotContext aotContext) {
389+
aotContext.typeConfiguration(type, config -> config.contributeAccessors().forQuerydsl());
390+
}
391+
392+
@Override
393+
protected @Nullable JpaRepositoryContributor contributeAotRepository(AotRepositoryContext repositoryContext) {
381394

382395
if (!repositoryContext.isGeneratedRepositoriesEnabled(MODULE_NAME)) {
383396
return null;

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/aot/AotContributionIntegrationTests.java

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,8 @@
3030
import org.springframework.context.aot.ApplicationContextAotGenerator;
3131
import org.springframework.core.io.InputStreamResource;
3232
import org.springframework.core.io.InputStreamSource;
33-
import org.springframework.data.aot.AotContext;
3433
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
3534
import org.springframework.data.jpa.repository.config.InfrastructureConfig;
36-
import org.springframework.mock.env.MockPropertySource;
3735

3836
/**
3937
* Integration tests for AOT processing.
@@ -70,8 +68,6 @@ void shouldGenerateMetadataForBaseRepositoryAndQuerydslFragment() throws IOExcep
7068
private static TestGenerationContext generate(Class<?>... configurationClasses) {
7169

7270
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
73-
context.getEnvironment().getPropertySources()
74-
.addFirst(new MockPropertySource().withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "true"));
7571
context.register(configurationClasses);
7672

7773
ApplicationContextAotGenerator generator = new ApplicationContextAotGenerator();

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

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import org.springframework.data.projection.ProjectionFactory;
4848
import org.springframework.data.projection.SpelAwareProxyProjectionFactory;
4949
import org.springframework.data.repository.config.AnnotationRepositoryConfigurationSource;
50+
import org.springframework.data.repository.config.RepositoryConfigurationSource;
5051
import org.springframework.data.repository.core.RepositoryMetadata;
5152
import org.springframework.data.repository.core.support.RepositoryComposition;
5253
import org.springframework.data.repository.core.support.RepositoryFactoryBeanSupport;
@@ -69,7 +70,8 @@ public class AotFragmentTestConfigurationSupport implements BeanFactoryPostProce
6970

7071
private final Class<?> repositoryInterface;
7172
private final boolean registerFragmentFacade;
72-
private final TestJpaAotRepositoryContext<?> repositoryContext;
73+
private final Class<?>[] additionalFragments;
74+
private final RepositoryConfigurationSource configSource;
7375

7476
public AotFragmentTestConfigurationSupport(Class<?> repositoryInterface) {
7577
this(repositoryInterface, SampleConfig.class, true);
@@ -82,22 +84,22 @@ public AotFragmentTestConfigurationSupport(Class<?> repositoryInterface, Class<?
8284
public AotFragmentTestConfigurationSupport(Class<?> repositoryInterface, Class<?> configClass,
8385
boolean registerFragmentFacade, Class<?>... additionalFragments) {
8486
this.repositoryInterface = repositoryInterface;
85-
86-
RepositoryComposition composition = RepositoryComposition
87-
.of((List) Arrays.stream(additionalFragments).map(RepositoryFragment::structural).toList());
88-
this.repositoryContext = new TestJpaAotRepositoryContext<>(repositoryInterface, composition,
89-
new AnnotationRepositoryConfigurationSource(AnnotationMetadata.introspect(configClass),
90-
EnableJpaRepositories.class, new DefaultResourceLoader(), new StandardEnvironment(),
91-
Mockito.mock(BeanDefinitionRegistry.class), DefaultBeanNameGenerator.INSTANCE));
9287
this.registerFragmentFacade = registerFragmentFacade;
88+
this.additionalFragments = additionalFragments;
89+
this.configSource = new AnnotationRepositoryConfigurationSource(AnnotationMetadata.introspect(configClass),
90+
EnableJpaRepositories.class, new DefaultResourceLoader(), new StandardEnvironment(),
91+
Mockito.mock(BeanDefinitionRegistry.class), DefaultBeanNameGenerator.INSTANCE);
9392
}
9493

9594
@Override
9695
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
9796

9897
TestGenerationContext generationContext = new TestGenerationContext(repositoryInterface);
98+
RepositoryComposition composition = RepositoryComposition
99+
.of((List) Arrays.stream(additionalFragments).map(RepositoryFragment::structural).toList());
99100

100-
repositoryContext.setBeanFactory(beanFactory);
101+
TestJpaAotRepositoryContext<?> repositoryContext = new TestJpaAotRepositoryContext<>(beanFactory,
102+
repositoryInterface, composition, configSource);
101103

102104
new JpaRepositoryContributor(repositoryContext).contribute(generationContext);
103105

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/aot/TestJpaAotRepositoryContext.java

Lines changed: 7 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,24 +19,20 @@
1919
import jakarta.persistence.MappedSuperclass;
2020

2121
import java.lang.annotation.Annotation;
22-
import java.util.Collection;
23-
import java.util.List;
2422
import java.util.Set;
25-
import java.util.function.Consumer;
2623

2724
import org.jspecify.annotations.Nullable;
2825

29-
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
26+
import org.springframework.beans.factory.BeanFactory;
3027
import org.springframework.core.annotation.MergedAnnotation;
31-
import org.springframework.core.env.Environment;
32-
import org.springframework.core.env.StandardEnvironment;
33-
import org.springframework.data.aot.AotTypeConfiguration;
28+
import org.springframework.data.aot.AotContext;
3429
import org.springframework.data.jpa.domain.sample.Role;
3530
import org.springframework.data.jpa.domain.sample.SpecialUser;
3631
import org.springframework.data.jpa.domain.sample.User;
3732
import org.springframework.data.jpa.repository.support.JpaRepositoryFragmentsContributor;
3833
import org.springframework.data.jpa.repository.support.SimpleJpaRepository;
3934
import org.springframework.data.repository.config.AotRepositoryContext;
35+
import org.springframework.data.repository.config.AotRepositoryContextSupport;
4036
import org.springframework.data.repository.config.AotRepositoryInformation;
4137
import org.springframework.data.repository.config.RepositoryConfigurationSource;
4238
import org.springframework.data.repository.core.RepositoryInformation;
@@ -49,15 +45,16 @@
4945
*
5046
* @author Christoph Strobl
5147
*/
52-
public class TestJpaAotRepositoryContext<T> implements AotRepositoryContext {
48+
public class TestJpaAotRepositoryContext<T> extends AotRepositoryContextSupport {
5349

5450
private final AotRepositoryInformation repositoryInformation;
5551
private final Class<T> repositoryInterface;
5652
private final RepositoryConfigurationSource configurationSource;
57-
private @Nullable ConfigurableListableBeanFactory beanFactory;
5853

59-
public TestJpaAotRepositoryContext(Class<T> repositoryInterface, @Nullable RepositoryComposition composition,
54+
public TestJpaAotRepositoryContext(BeanFactory beanFactory, Class<T> repositoryInterface,
55+
@Nullable RepositoryComposition composition,
6056
RepositoryConfigurationSource configurationSource) {
57+
super(AotContext.from(beanFactory));
6158
this.repositoryInterface = repositoryInterface;
6259
this.configurationSource = configurationSource;
6360

@@ -69,45 +66,6 @@ public TestJpaAotRepositoryContext(Class<T> repositoryInterface, @Nullable Repos
6966
composition.append(fragments).getFragments().stream().toList());
7067
}
7168

72-
public Class<T> getRepositoryInterface() {
73-
return repositoryInterface;
74-
}
75-
76-
@Override
77-
public ConfigurableListableBeanFactory getBeanFactory() {
78-
return beanFactory;
79-
}
80-
81-
@Override
82-
public Environment getEnvironment() {
83-
return new StandardEnvironment();
84-
}
85-
86-
@Override
87-
public TypeIntrospector introspectType(String typeName) {
88-
return null;
89-
}
90-
91-
@Override
92-
public IntrospectedBeanDefinition introspectBeanDefinition(String beanName) {
93-
return null;
94-
}
95-
96-
@Override
97-
public void typeConfiguration(Class<?> type, Consumer<AotTypeConfiguration> configurationConsumer) {
98-
99-
}
100-
101-
@Override
102-
public Collection<AotTypeConfiguration> typeConfigurations() {
103-
return List.of();
104-
}
105-
106-
@Override
107-
public String getBeanName() {
108-
return "dummyRepository";
109-
}
110-
11169
@Override
11270
public String getModuleName() {
11371
return "JPA";
@@ -118,11 +76,6 @@ public RepositoryConfigurationSource getConfigurationSource() {
11876
return configurationSource;
11977
}
12078

121-
@Override
122-
public Set<String> getBasePackages() {
123-
return Set.of("org.springframework.data.dummy.repository.aot");
124-
}
125-
12679
@Override
12780
public Set<Class<? extends Annotation>> getIdentifyingAnnotations() {
12881
return Set.of(Entity.class, MappedSuperclass.class);
@@ -143,12 +96,4 @@ public Set<Class<?>> getResolvedTypes() {
14396
return Set.of(User.class, SpecialUser.class, Role.class);
14497
}
14598

146-
@Override
147-
public Set<Class<?>> getUserDomainTypes() {
148-
return Set.of();
149-
}
150-
151-
public void setBeanFactory(ConfigurableListableBeanFactory beanFactory) {
152-
this.beanFactory = beanFactory;
153-
}
15499
}

0 commit comments

Comments
 (0)