Skip to content

Commit 7e754ce

Browse files
committed
Polishing.
Encapsulate config, add details about ant-path-style matching.
1 parent 4a9b9ab commit 7e754ce

File tree

2 files changed

+63
-23
lines changed

2 files changed

+63
-23
lines changed

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

Lines changed: 15 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,24 +23,28 @@ If Ahead of Time compilation is enabled Spring Data can (depending on the actual
2323
* Repository Metadata in JSON format
2424

2525
Each of the above is enabled by default.
26-
However there users may fine tune the configuration with following options.
26+
However, there users may fine tune the configuration with following options.
2727

2828
[options = "autowidth",cols="1,1"]
2929
|===
30-
|spring.aot.data.accessors.enabled
31-
|boolean flag to control contribution of Bytecode for generated Type/Property Accessors
30+
|`spring.aot.data.accessors.enabled`
31+
|Boolean flag to control contribution of Bytecode for generated Type/Property Accessors
3232

33-
|spring.aot.data.accessors.exclude
34-
|comma separated list of FQCN for which to skip contribution of Bytecode for generated Type/Property Accessors
33+
|`spring.aot.data.accessors.include`
34+
|Comma separated list of FQCN for which to contribute Bytecode for generated Type/Property Accessors.
35+
Ant-style include patterns matching package names (e.g. `com.acme.**`) or type names inclusion.
36+
Inclusion pattern matches are evaluated before exclusions for broad exclusion and selective inclusion.
3537

36-
|spring.aot.data.accessors.include
37-
|comma separated list of FQCN for which to contribute Bytecode for generated Type/Property Accessors
38+
|`spring.aot.data.accessors.exclude`
39+
|Comma separated list of FQCN for which to skip contribution of Bytecode for generated Type/Property Accessors.
40+
Ant-style exclude patterns matching package names (e.g. `com.acme.**`) or type names exclusion.
41+
Exclusion pattern matches are evaluated after inclusions for broad exclusion and selective inclusion.
3842

39-
|spring.aot.repositories.enabled
40-
|boolean flag to control contribution of Source Code for Repository Interfaces
43+
|`spring.aot.repositories.enabled`
44+
|Boolean flag to control contribution of Source Code for Repository Interfaces
4145

42-
|spring.aot.[module-name].repositories.enabled
43-
|boolean flag to control contribution of Source Code for Repository Interfaces for a certain module (eg. jdbc)
46+
|`spring.aot.[module-name].repositories.enabled`
47+
|Boolean flag to control contribution of Source Code for Repository Interfaces for a certain module (eg. `jdbc`, `jpa`, `mongodb`, `cassandra`)
4448
|===
4549

4650
[[aot.repositories]]

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

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.Optional;
2727
import java.util.Set;
2828
import java.util.function.Consumer;
29+
import java.util.function.Supplier;
2930
import java.util.stream.Stream;
3031

3132
import org.jspecify.annotations.Nullable;
@@ -39,6 +40,7 @@
3940
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
4041
import org.springframework.beans.factory.support.RootBeanDefinition;
4142
import org.springframework.core.env.Environment;
43+
import org.springframework.data.util.Lazy;
4244
import org.springframework.data.util.QTypeContributor;
4345
import org.springframework.data.util.TypeContributor;
4446
import org.springframework.util.AntPathMatcher;
@@ -234,11 +236,8 @@ public void contribute(Environment environment, GenerationContext generationCont
234236

235237
if (contributeAccessors) {
236238

237-
boolean accessorsEnabled = environment.getProperty("spring.aot.data.accessors.enabled", Boolean.class, true);
238-
String include = environment.getProperty("spring.aot.data.accessors.include", String.class, "");
239-
String exclude = environment.getProperty("spring.aot.data.accessors.exclude", String.class, "");
240-
241-
if (shouldContributeAccessors(type, accessorsEnabled, include, exclude)) {
239+
AccessorContributionConfiguration configuration = AccessorContributionConfiguration.of(environment);
240+
if (configuration.shouldContributeAccessors(type)) {
242241
mappingContext.contribute(type);
243242
}
244243
}
@@ -257,20 +256,55 @@ public void contribute(Environment environment, GenerationContext generationCont
257256
Stream.concat(Stream.of(TypeReference.of(type)), proxyInterfaces.stream()).toArray(TypeReference[]::new));
258257
}
259258
}
259+
}
260260

261+
}
262+
263+
/**
264+
* Configuration for accessor to determine whether accessors should be contributed for a given type.
265+
*/
266+
private record AccessorContributionConfiguration(boolean enabled, Lazy<String> include, Lazy<String> exclude) {
267+
268+
/**
269+
* {@code boolean }Environment property to enable/disable accessor contribution. Enabled by default.
270+
*/
271+
public static final String ACCESSORS_ENABLED = "spring.aot.data.accessors.enabled";
272+
273+
/**
274+
* {@code String} Environment property to define Ant-style include patterns (comma-separated) matching package names
275+
* (e.g. {@code com.acme.**}) or type names inclusion. Inclusion pattern matches are evaluated before exclusions for
276+
* broad exclusion and selective inclusion.
277+
*/
278+
public static final String INCLUDE_PATTERNS = "spring.aot.data.accessors.include";
279+
280+
/**
281+
* {@code String} Environment property to define Ant-style exclude patterns (comma-separated) matching package names
282+
* (e.g. {@code com.acme.**}) or type names exclusion. Exclusion pattern matches are evaluated after inclusions for
283+
* broad exclusion and selective inclusion.
284+
*/
285+
public static final String EXCLUDE_PATTERNS = "spring.aot.data.accessors.exclude";
286+
287+
private static final AntPathMatcher antPathMatcher = new AntPathMatcher(".");
288+
289+
private AccessorContributionConfiguration(boolean enabled, Supplier<String> include, Supplier<String> exclude) {
290+
this(enabled, Lazy.of(include), Lazy.of(exclude));
261291
}
262292

263-
static boolean shouldContributeAccessors(Class<?> type, boolean enabled, String include, String exclude) {
293+
public static AccessorContributionConfiguration of(Environment environment) {
294+
return new AccessorContributionConfiguration(environment.getProperty(ACCESSORS_ENABLED, Boolean.class, true),
295+
() -> environment.getProperty(INCLUDE_PATTERNS, String.class, ""),
296+
() -> environment.getProperty(EXCLUDE_PATTERNS, String.class, ""));
297+
}
298+
299+
boolean shouldContributeAccessors(Class<?> type) {
264300

265301
if (!enabled) {
266302
return false;
267303
}
268304

269-
AntPathMatcher antPathMatcher = new AntPathMatcher(".");
270-
271-
if (StringUtils.hasText(include)) {
305+
if (StringUtils.hasText(include.get())) {
272306

273-
String[] includes = include.split(",");
307+
String[] includes = include.get().split(",");
274308

275309
for (String includePattern : includes) {
276310
if (antPathMatcher.match(includePattern.trim(), type.getName())) {
@@ -279,9 +313,9 @@ static boolean shouldContributeAccessors(Class<?> type, boolean enabled, String
279313
}
280314
}
281315

282-
if (StringUtils.hasText(exclude)) {
316+
if (StringUtils.hasText(exclude.get())) {
283317

284-
String[] excludes = exclude.split(",");
318+
String[] excludes = exclude.get().split(",");
285319

286320
for (String excludePattern : excludes) {
287321
if (antPathMatcher.match(excludePattern.trim(), type.getName())) {
@@ -292,5 +326,7 @@ static boolean shouldContributeAccessors(Class<?> type, boolean enabled, String
292326

293327
return true;
294328
}
329+
295330
}
331+
296332
}

0 commit comments

Comments
 (0)