2626import java .util .Optional ;
2727import java .util .Set ;
2828import java .util .function .Consumer ;
29+ import java .util .function .Supplier ;
2930import java .util .stream .Stream ;
3031
3132import org .jspecify .annotations .Nullable ;
3940import org .springframework .beans .factory .support .DefaultListableBeanFactory ;
4041import org .springframework .beans .factory .support .RootBeanDefinition ;
4142import org .springframework .core .env .Environment ;
43+ import org .springframework .data .util .Lazy ;
4244import org .springframework .data .util .QTypeContributor ;
4345import org .springframework .data .util .TypeContributor ;
4446import 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