Skip to content

Commit a53eb8b

Browse files
HyunSangHanmp911de
authored andcommitted
Enable AOT repository generation by default.
Signed-off-by: Hyunsang Han <[email protected]> Original pull request: #3904 Closes #3899
1 parent d1dc6dc commit a53eb8b

File tree

5 files changed

+104
-10
lines changed

5 files changed

+104
-10
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
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>
4041
<mysql-connector-java>9.2.0</mysql-connector-java>
4142
<postgresql>42.7.7</postgresql>
4243
<oracle>23.8.0.25.04</oracle>
@@ -175,6 +176,12 @@
175176
<type>pom</type>
176177
<scope>import</scope>
177178
</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>
178185
</dependencies>
179186
</dependencyManagement>
180187

spring-data-jpa/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,12 @@
100100
<scope>test</scope>
101101
</dependency>
102102

103+
<dependency>
104+
<groupId>org.junit-pioneer</groupId>
105+
<artifactId>junit-pioneer</artifactId>
106+
<scope>test</scope>
107+
</dependency>
108+
103109
<dependency>
104110
<groupId>org.springframework</groupId>
105111
<artifactId>spring-core-test</artifactId>

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

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

43+
import org.springframework.aot.AotDetector;
4344
import org.springframework.aot.generate.GenerationContext;
4445
import org.springframework.beans.factory.ObjectProvider;
4546
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
@@ -92,6 +93,7 @@
9293
* @author Thomas Darimont
9394
* @author Christoph Strobl
9495
* @author Mark Paluch
96+
* @author Hyunsang Han
9597
*/
9698
public class JpaRepositoryConfigExtension extends RepositoryConfigurationExtensionSupport {
9799

@@ -379,8 +381,10 @@ public static class JpaRepositoryRegistrationAotProcessor extends RepositoryRegi
379381

380382
Environment environment = repositoryContext.getEnvironment();
381383

384+
String enabledByDefault = AotDetector.useGeneratedArtifacts() ? "true" : "false";
385+
382386
boolean enabled = Boolean
383-
.parseBoolean(environment.getProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "false"));
387+
.parseBoolean(environment.getProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, enabledByDefault));
384388
if (!enabled) {
385389
return null;
386390
}

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

Lines changed: 80 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
import org.jspecify.annotations.Nullable;
3131
import org.junit.jupiter.api.Test;
3232

33+
import org.junitpioneer.jupiter.ClearSystemProperty;
34+
import org.junitpioneer.jupiter.SetSystemProperty;
35+
import org.springframework.aot.AotDetector;
3336
import org.springframework.aot.generate.ClassNameGenerator;
3437
import org.springframework.aot.generate.DefaultGenerationContext;
3538
import org.springframework.aot.generate.GenerationContext;
@@ -56,14 +59,14 @@
5659

5760
/**
5861
* @author Christoph Strobl
62+
* @author Hyunsang Han
5963
*/
6064
class JpaRepositoryRegistrationAotProcessorUnitTests {
6165

6266
@Test // GH-2628
6367
void aotProcessorMustNotRegisterDomainTypes() {
6468

65-
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
66-
new InMemoryGeneratedFiles());
69+
GenerationContext ctx = createGenerationContext();
6770

6871
new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
6972
.contribute(new DummyAotRepositoryContext(null) {
@@ -79,8 +82,7 @@ public Set<Class<?>> getResolvedTypes() {
7982
@Test // GH-2628
8083
void aotProcessorMustNotRegisterAnnotations() {
8184

82-
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
83-
new InMemoryGeneratedFiles());
85+
GenerationContext ctx = createGenerationContext();
8486

8587
new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
8688
.contribute(new DummyAotRepositoryContext(null) {
@@ -99,8 +101,7 @@ public Set<MergedAnnotation<Annotation>> getResolvedAnnotations() {
99101
@Test // GH-3838
100102
void repositoryProcessorShouldConsiderPersistenceManagedTypes() {
101103

102-
GenerationContext ctx = new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
103-
new InMemoryGeneratedFiles());
104+
GenerationContext ctx = createGenerationContext();
104105

105106
GenericApplicationContext context = new GenericApplicationContext();
106107
context.registerBean(PersistenceManagedTypes.class, () -> {
@@ -126,12 +127,83 @@ public List<String> getManagedPackages() {
126127
context.getEnvironment().getPropertySources()
127128
.addFirst(new MockPropertySource().withProperty(AotContext.GENERATED_REPOSITORIES_ENABLED, "true"));
128129

129-
JpaRepositoryContributor contributor = new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
130-
.contribute(new DummyAotRepositoryContext(context), ctx);
130+
JpaRepositoryContributor contributor = createContributor(new DummyAotRepositoryContext(context), ctx);
131131

132132
assertThat(contributor.getMetamodel().managedType(Person.class)).isNotNull();
133133
}
134134

135+
@Test // GH-3899
136+
@SetSystemProperty(key = AotDetector.AOT_ENABLED, value = "true")
137+
void repositoryProcessorShouldEnableAotRepositoriesByDefaultWhenAotIsEnabled() {
138+
139+
GenerationContext ctx = createGenerationContext();
140+
GenericApplicationContext context = createApplicationContext();
141+
142+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
143+
144+
assertThat(contributor).isNotNull();
145+
}
146+
147+
@Test // GH-3899
148+
@ClearSystemProperty(key = AotDetector.AOT_ENABLED)
149+
void repositoryProcessorShouldNotEnableAotRepositoriesByDefaultWhenAotIsDisabled() {
150+
151+
GenerationContext ctx = createGenerationContext();
152+
GenericApplicationContext context = createApplicationContext();
153+
154+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
155+
156+
assertThat(contributor).isNull();
157+
}
158+
159+
@Test // GH-3899
160+
@SetSystemProperty(key = AotDetector.AOT_ENABLED, value = "true")
161+
@SetSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED, value = "false")
162+
void repositoryProcessorShouldRespectExplicitRepositoryEnabledProperty() {
163+
164+
GenerationContext ctx = createGenerationContext();
165+
GenericApplicationContext context = createApplicationContext();
166+
167+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
168+
169+
assertThat(contributor).isNull();
170+
}
171+
172+
@Test // GH-3899
173+
@SetSystemProperty(key = AotContext.GENERATED_REPOSITORIES_ENABLED, value = "true")
174+
void repositoryProcessorShouldEnableWhenExplicitlySetToTrue() {
175+
176+
GenerationContext ctx = createGenerationContext();
177+
GenericApplicationContext context = createApplicationContext();
178+
179+
JpaRepositoryContributor contributor = createContributorWithPersonTypes(context, ctx);
180+
181+
assertThat(contributor).isNotNull();
182+
}
183+
184+
private GenerationContext createGenerationContext() {
185+
return new DefaultGenerationContext(new ClassNameGenerator(ClassName.OBJECT),
186+
new InMemoryGeneratedFiles());
187+
}
188+
189+
private GenericApplicationContext createApplicationContext() {
190+
return new GenericApplicationContext();
191+
}
192+
193+
private JpaRepositoryContributor createContributor(AotRepositoryContext repositoryContext, GenerationContext ctx) {
194+
return new JpaRepositoryConfigExtension.JpaRepositoryRegistrationAotProcessor()
195+
.contribute(repositoryContext, ctx);
196+
}
197+
198+
private JpaRepositoryContributor createContributorWithPersonTypes(GenericApplicationContext context, GenerationContext ctx) {
199+
return createContributor(new DummyAotRepositoryContext(context) {
200+
@Override
201+
public Set<Class<?>> getResolvedTypes() {
202+
return Collections.singleton(Person.class);
203+
}
204+
}, ctx);
205+
}
206+
135207
@Entity
136208
static class Person {
137209
@Id Long id;

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,12 @@ Do not use them directly in your code as generation and implementation details m
4444
=== Running with AOT Repositories
4545

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.
47-
It is also possible to use those optimizations on the JVM by setting the `spring.aot.enabled` and `spring.aot.repositories.enabled` properties to `true`.
47+
When AOT is enabled (either for native compilation or by setting `spring.aot.enabled=true`), AOT repositories are automatically enabled by default.
48+
49+
You can explicitly control AOT repository generation by setting the `spring.aot.repositories.enabled` property:
50+
51+
* `spring.aot.repositories.enabled=true` - Explicitly enable AOT repositories
52+
* `spring.aot.repositories.enabled=false` - Disable AOT repositories even when AOT is enabled
4853

4954
AOT repositories contribute configuration changes to the actual repository bean registration to register the generated repository fragment.
5055

0 commit comments

Comments
 (0)