Skip to content

Commit ccaa32b

Browse files
committed
Polishing.
Refine documentation wording. Use build-managed JUnit pioneer version property. Use Commons-infrastructure for checking whether AOT repositories are enabled. Original pull request: #3904 See #3899
1 parent a53eb8b commit ccaa32b

File tree

5 files changed

+39
-50
lines changed

5 files changed

+39
-50
lines changed

pom.xml

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
<h2>2.3.232</h2>
3838
<jakarta-persistence-api>3.2.0</jakarta-persistence-api>
3939
<jsqlparser>5.2</jsqlparser>
40-
<junit-pioneer>2.3.0</junit-pioneer>
4140
<mysql-connector-java>9.2.0</mysql-connector-java>
4241
<postgresql>42.7.7</postgresql>
4342
<oracle>23.8.0.25.04</oracle>
@@ -176,12 +175,6 @@
176175
<type>pom</type>
177176
<scope>import</scope>
178177
</dependency>
179-
<dependency>
180-
<groupId>org.junit-pioneer</groupId>
181-
<artifactId>junit-pioneer</artifactId>
182-
<version>${junit-pioneer}</version>
183-
<scope>test</scope>
184-
</dependency>
185178
</dependencies>
186179
</dependencyManagement>
187180

spring-data-jpa/pom.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@
103103
<dependency>
104104
<groupId>org.junit-pioneer</groupId>
105105
<artifactId>junit-pioneer</artifactId>
106+
<version>${junit-pioneer}</version>
106107
<scope>test</scope>
107108
</dependency>
108109

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

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@
4040
import org.slf4j.Logger;
4141
import org.slf4j.LoggerFactory;
4242

43-
import org.springframework.aot.AotDetector;
4443
import org.springframework.aot.generate.GenerationContext;
4544
import org.springframework.beans.factory.ObjectProvider;
4645
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
@@ -59,7 +58,6 @@
5958
import org.springframework.core.io.ResourceLoader;
6059
import org.springframework.dao.DataAccessException;
6160
import org.springframework.dao.annotation.PersistenceExceptionTranslationPostProcessor;
62-
import org.springframework.data.aot.AotContext;
6361
import org.springframework.data.jpa.repository.JpaRepository;
6462
import org.springframework.data.jpa.repository.aot.JpaRepositoryContributor;
6563
import org.springframework.data.jpa.repository.support.DefaultJpaContext;
@@ -363,6 +361,7 @@ static boolean isActive(@Nullable ClassLoader classLoader) {
363361
return AGENT_CLASSES.stream() //
364362
.anyMatch(agentClass -> ClassUtils.isPresent(agentClass, classLoader));
365363
}
364+
366365
}
367366

368367
/**
@@ -374,25 +373,19 @@ static boolean isActive(@Nullable ClassLoader classLoader) {
374373
*/
375374
public static class JpaRepositoryRegistrationAotProcessor extends RepositoryRegistrationAotProcessor {
376375

377-
String GENERATED_REPOSITORIES_JPA_USE_ENTITY_MANAGER = "spring.aot.jpa.repositories.use-entitymanager";
376+
private static final String USE_ENTITY_MANAGER = "spring.aot.jpa.repositories.use-entitymanager";
377+
private static final String MODULE_NAME = "jpa";
378378

379379
protected @Nullable JpaRepositoryContributor contribute(AotRepositoryContext repositoryContext,
380380
GenerationContext generationContext) {
381381

382-
Environment environment = repositoryContext.getEnvironment();
383-
384-
String enabledByDefault = AotDetector.useGeneratedArtifacts() ? "true" : "false";
385-
386-
boolean enabled = Boolean
387-
.parseBoolean(environment.getProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, enabledByDefault));
388-
if (!enabled) {
382+
if (!repositoryContext.isGeneratedRepositoriesEnabled(MODULE_NAME)) {
389383
return null;
390384
}
391385

392386
ConfigurableListableBeanFactory beanFactory = repositoryContext.getBeanFactory();
393-
394-
boolean useEntityManager = Boolean
395-
.parseBoolean(environment.getProperty(GENERATED_REPOSITORIES_JPA_USE_ENTITY_MANAGER, "false"));
387+
Environment environment = repositoryContext.getEnvironment();
388+
boolean useEntityManager = environment.getProperty(USE_ENTITY_MANAGER, Boolean.class, false);
396389

397390
if (useEntityManager) {
398391

@@ -430,5 +423,7 @@ public static class JpaRepositoryRegistrationAotProcessor extends RepositoryRegi
430423
log.debug("Using scanned types for AOT repository generation");
431424
return new JpaRepositoryContributor(repositoryContext);
432425
}
426+
433427
}
428+
434429
}

spring-data-jpa/src/test/java/org/springframework/data/jpa/repository/config/JpaRepositoryRegistrationAotProcessorUnitTests.java

Lines changed: 27 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@
2929

3030
import org.jspecify.annotations.Nullable;
3131
import org.junit.jupiter.api.Test;
32-
3332
import org.junitpioneer.jupiter.ClearSystemProperty;
3433
import org.junitpioneer.jupiter.SetSystemProperty;
34+
3535
import org.springframework.aot.AotDetector;
3636
import org.springframework.aot.generate.ClassNameGenerator;
3737
import org.springframework.aot.generate.DefaultGenerationContext;
@@ -58,18 +58,22 @@
5858
import org.springframework.orm.jpa.persistenceunit.PersistenceManagedTypes;
5959

6060
/**
61+
* Unit tests for {@link JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor}.
62+
*
6163
* @author Christoph Strobl
6264
* @author Hyunsang Han
65+
* @author Mark Paluch
6366
*/
6467
class JpaRepositoryRegistrationAotProcessorUnitTests {
6568

6669
@Test // GH-2628
6770
void aotProcessorMustNotRegisterDomainTypes() {
6871

6972
GenerationContext ctx = createGenerationContext();
73+
GenericApplicationContext context = new GenericApplicationContext();
7074

7175
new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
72-
.contribute(new DummyAotRepositoryContext(null) {
76+
.contribute(new DummyAotRepositoryContext(context) {
7377
@Override
7478
public Set<Class<?>> getResolvedTypes() {
7579
return Collections.singleton(Person.class);
@@ -84,8 +88,9 @@ void aotProcessorMustNotRegisterAnnotations() {
8488

8589
GenerationContext ctx = createGenerationContext();
8690

91+
GenericApplicationContext context = new GenericApplicationContext();
8792
new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
88-
.contribute(new DummyAotRepositoryContext(null) {
93+
.contribute(new DummyAotRepositoryContext(context) {
8994

9095
@Override
9196
public Set<MergedAnnotation<Annotation>> getResolvedAnnotations() {
@@ -127,7 +132,8 @@ public List<String> getManagedPackages() {
127132
context.getEnvironment().getPropertySources()
128133
.addFirst(new MockPropertySource().withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "true"));
129134

130-
JpaRepositoryContributor contributor = createContributor(new DummyAotRepositoryContext(context), ctx);
135+
JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
136+
.contribute(new DummyAotRepositoryContext(context), ctx);
131137

132138
assertThat(contributor.getMetamodel().managedType(Person.class)).isNotNull();
133139
}
@@ -137,66 +143,58 @@ public List<String> getManagedPackages() {
137143
void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() {
138144

139145
GenerationContext ctx = createGenerationContext();
140-
GenericApplicationContext context = createApplicationContext();
146+
GenericApplicationContext context = new GenericApplicationContext();
141147

142148
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
143149

144150
assertThat(contributor).isNotNull();
145151
}
146152

147153
@Test // GH-3899
148-
@ClearSystemProperty(key = AotDetector.AOT_ENABLED)
149-
void repositoryProcessorShouldNotEnableAotRepositoriesByDefaultWhenAotIsDisabled() {
154+
@ClearSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED)
155+
void shouldEnableAotRepositoriesByDefault() {
150156

151157
GenerationContext ctx = createGenerationContext();
152-
GenericApplicationContext context = createApplicationContext();
158+
GenericApplicationContext context = new GenericApplicationContext();
153159

154160
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
155161

156-
assertThat(contributor).isNull();
162+
assertThat(contributor).isNotNull();
157163
}
158164

159165
@Test // GH-3899
160-
@SetSystemProperty(key = AotDetector.AOT_ENABLED, value = "true")
161166
@SetSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED, value = "false")
162-
void repositoryProcessorShouldRespectExplicitRepositoryEnabledProperty() {
167+
void shouldDisableAotRepositoriesWhenGeneratedRepositoriesIsFalse() {
163168

164169
GenerationContext ctx = createGenerationContext();
165-
GenericApplicationContext context = createApplicationContext();
170+
GenericApplicationContext context = new GenericApplicationContext();
166171

167172
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
168173

169174
assertThat(contributor).isNull();
170175
}
171176

172177
@Test // GH-3899
173-
@SetSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED, value = "true")
174-
void repositoryProcessorShouldEnableWhenExplicitlySetToTrue() {
178+
@SetSystemProperty(key = "spring.aot.jpa.repositories.enabled", value = "false")
179+
void shouldDisableAotRepositoriesWhenJpaGeneratedRepositoriesIsFalse() {
175180

176181
GenerationContext ctx = createGenerationContext();
177-
GenericApplicationContext context = createApplicationContext();
182+
GenericApplicationContext context = new GenericApplicationContext();
178183

179184
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
180185

181-
assertThat(contributor).isNotNull();
186+
assertThat(contributor).isNull();
182187
}
183188

184189
private GenerationContext createGenerationContext() {
185190
return new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
186191
new InMemoryGeneratedFiles());
187192
}
188193

189-
private GenericApplicationContext createApplicationContext() {
190-
return new GenericApplicationContext();
191-
}
194+
private JpaRepositoryContributor createContributorWithPersonTypes(GenericApplicationContext context, GenerationContext ctx) {
192195

193-
private JpaRepositoryContributor createContributor(AotRepositoryContext repositoryContext, GenerationContext ctx) {
194196
return new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
195-
.contribute(repositoryContext, ctx);
196-
}
197-
198-
private JpaRepositoryContributor createContributorWithPersonTypes(GenericApplicationContext context, GenerationContext ctx) {
199-
return createContributor(new DummyAotRepositoryContext(context) {
197+
.contribute(new DummyAotRepositoryContext(context) {
200198
@Override
201199
public Set<Class<?>> getResolvedTypes() {
202200
return Collections.singleton(Person.class);
@@ -252,12 +250,12 @@ public RepositoryInformation getRepositoryInformation() {
252250

253251
@Override
254252
public Set<MergedAnnotation<Annotation>> getResolvedAnnotations() {
255-
return null;
253+
return Set.of();
256254
}
257255

258256
@Override
259257
public Set<Class<?>> getResolvedTypes() {
260-
return null;
258+
return Set.of();
261259
}
262260

263261
@Override
@@ -279,5 +277,7 @@ public TypeIntrospector introspectType(String typeName) {
279277
public IntrospectedBeanDefinition introspectBeanDefinition(String beanName) {
280278
return null;
281279
}
280+
282281
}
282+
283283
}

src/main/antora/modules/ROOT/pages/jpa/aot.adoc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ Do not use them directly in your code as generation and implementation details m
4646
AOT is a mandatory step to transform a Spring application to a native executable, so it is automatically enabled when running in this mode.
4747
When AOT is enabled (either for native compilation or by setting `spring.aot.enabled=true`), AOT repositories are automatically enabled by default.
4848

49-
You can explicitly control AOT repository generation by setting the `spring.aot.repositories.enabled` property:
49+
You can disable AOT repository generation entirely or only disable JPA AOT repositories:
5050

51-
* `spring.aot.repositories.enabled=true` - Explicitly enable AOT repositories
52-
* `spring.aot.repositories.enabled=false` - Disable AOT repositories even when AOT is enabled
51+
* Set the `spring.aot.repositories.enabled=false` property to disable generated repositories for all Spring Data modules.
52+
* Set the `spring.aot.jpa.repositories.enabled=false` property to disable only JPA AOT repositories.
5353

5454
AOT repositories contribute configuration changes to the actual repository bean registration to register the generated repository fragment.
5555

0 commit comments

Comments
 (0)