27
27
import java .util .LinkedHashSet ;
28
28
import java .util .List ;
29
29
import java .util .Map ;
30
- import java .util .Objects ;
31
30
import java .util .Set ;
31
+ import java .util .function .Function ;
32
32
import java .util .function .Predicate ;
33
33
import java .util .function .Supplier ;
34
34
84
84
import org .springframework .core .env .ConfigurableEnvironment ;
85
85
import org .springframework .core .env .Environment ;
86
86
import org .springframework .core .env .StandardEnvironment ;
87
+ import org .springframework .core .io .ClassPathResource ;
87
88
import org .springframework .core .io .DefaultResourceLoader ;
89
+ import org .springframework .core .io .Resource ;
88
90
import org .springframework .core .io .ResourceLoader ;
89
91
import org .springframework .core .io .support .PropertySourceDescriptor ;
90
92
import org .springframework .core .io .support .PropertySourceProcessor ;
@@ -328,7 +330,8 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
328
330
if (hasPropertySourceDescriptors || hasImportRegistry ) {
329
331
return (generationContext , code ) -> {
330
332
if (hasPropertySourceDescriptors ) {
331
- new PropertySourcesAotContribution (this .propertySourceDescriptors ).applyTo (generationContext , code );
333
+ new PropertySourcesAotContribution (this .propertySourceDescriptors , this ::resolvePropertySourceLocation )
334
+ .applyTo (generationContext , code );
332
335
}
333
336
if (hasImportRegistry ) {
334
337
new ImportAwareAotContribution (beanFactory ).applyTo (generationContext , code );
@@ -338,6 +341,18 @@ public BeanFactoryInitializationAotContribution processAheadOfTime(ConfigurableL
338
341
return null ;
339
342
}
340
343
344
+ @ Nullable
345
+ private Resource resolvePropertySourceLocation (String location ) {
346
+ try {
347
+ String resolvedLocation = (this .environment != null
348
+ ? this .environment .resolveRequiredPlaceholders (location ) : location );
349
+ return this .resourceLoader .getResource (resolvedLocation );
350
+ }
351
+ catch (Exception ex ) {
352
+ return null ;
353
+ }
354
+ }
355
+
341
356
/**
342
357
* Build and validate a configuration model based on the registry of
343
358
* {@link Configuration} classes.
@@ -646,8 +661,11 @@ private static class PropertySourcesAotContribution implements BeanFactoryInitia
646
661
647
662
private final List <PropertySourceDescriptor > descriptors ;
648
663
649
- PropertySourcesAotContribution (List <PropertySourceDescriptor > descriptors ) {
664
+ private final Function <String , Resource > resourceResolver ;
665
+
666
+ PropertySourcesAotContribution (List <PropertySourceDescriptor > descriptors , Function <String , Resource > resourceResolver ) {
650
667
this .descriptors = descriptors ;
668
+ this .resourceResolver = resourceResolver ;
651
669
}
652
670
653
671
@ Override
@@ -661,10 +679,18 @@ public void applyTo(GenerationContext generationContext, BeanFactoryInitializati
661
679
}
662
680
663
681
private void registerRuntimeHints (RuntimeHints hints ) {
664
- this .descriptors .stream ().map (PropertySourceDescriptor ::propertySourceFactory )
665
- .filter (Objects ::nonNull ).distinct ()
666
- .forEach (factory -> hints .reflection ()
667
- .registerType (factory , MemberCategory .INVOKE_DECLARED_CONSTRUCTORS ));
682
+ for (PropertySourceDescriptor descriptor : this .descriptors ) {
683
+ Class <?> factory = descriptor .propertySourceFactory ();
684
+ if (factory != null ) {
685
+ hints .reflection ().registerType (factory , MemberCategory .INVOKE_DECLARED_CONSTRUCTORS );
686
+ }
687
+ for (String location : descriptor .locations ()) {
688
+ Resource resource = this .resourceResolver .apply (location );
689
+ if (resource != null && resource .exists () && resource instanceof ClassPathResource classpathResource ) {
690
+ hints .resources ().registerPattern (classpathResource .getPath ());
691
+ }
692
+ }
693
+ }
668
694
}
669
695
670
696
private void generateAddPropertySourceProcessorMethod (MethodSpec .Builder method ) {
0 commit comments