26
26
import java .util .Optional ;
27
27
import java .util .Set ;
28
28
import java .util .function .Consumer ;
29
+ import java .util .function .Supplier ;
29
30
import java .util .stream .Stream ;
30
31
31
32
import org .jspecify .annotations .Nullable ;
39
40
import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
40
41
import org .springframework .beans .factory .support .RootBeanDefinition ;
41
42
import org .springframework .core .env .Environment ;
43
+ import org .springframework .data .util .Lazy ;
42
44
import org .springframework .data .util .QTypeContributor ;
43
45
import org .springframework .data .util .TypeContributor ;
44
46
import org .springframework .util .AntPathMatcher ;
@@ -234,11 +236,8 @@ public void contribute(Environment environment, GenerationContext generationCont
234
236
235
237
if (contributeAccessors ) {
236
238
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 )) {
242
241
mappingContext .contribute (type );
243
242
}
244
243
}
@@ -257,20 +256,55 @@ public void contribute(Environment environment, GenerationContext generationCont
257
256
Stream .concat (Stream .of (TypeReference .of (type )), proxyInterfaces .stream ()).toArray (TypeReference []::new ));
258
257
}
259
258
}
259
+ }
260
260
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 ));
261
291
}
262
292
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 ) {
264
300
265
301
if (!enabled ) {
266
302
return false ;
267
303
}
268
304
269
- AntPathMatcher antPathMatcher = new AntPathMatcher ("." );
270
-
271
- if (StringUtils .hasText (include )) {
305
+ if (StringUtils .hasText (include .get ())) {
272
306
273
- String [] includes = include .split ("," );
307
+ String [] includes = include .get (). split ("," );
274
308
275
309
for (String includePattern : includes ) {
276
310
if (antPathMatcher .match (includePattern .trim (), type .getName ())) {
@@ -279,9 +313,9 @@ static boolean shouldContributeAccessors(Class<?> type, boolean enabled, String
279
313
}
280
314
}
281
315
282
- if (StringUtils .hasText (exclude )) {
316
+ if (StringUtils .hasText (exclude . get () )) {
283
317
284
- String [] excludes = exclude .split ("," );
318
+ String [] excludes = exclude .get (). split ("," );
285
319
286
320
for (String excludePattern : excludes ) {
287
321
if (antPathMatcher .match (excludePattern .trim (), type .getName ())) {
@@ -292,5 +326,7 @@ static boolean shouldContributeAccessors(Class<?> type, boolean enabled, String
292
326
293
327
return true ;
294
328
}
329
+
295
330
}
331
+
296
332
}
0 commit comments