Browse Source

Merge branch '6.0.x'

pull/31158/head
Sam Brannen 3 years ago
parent
commit
a96aeb69c1
  1. 33
      spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

33
spring-context/src/main/java/org/springframework/context/annotation/ConfigurationClassPostProcessor.java

@ -344,8 +344,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
@Nullable @Nullable
private Resource resolvePropertySourceLocation(String location) { private Resource resolvePropertySourceLocation(String location) {
try { try {
String resolvedLocation = (this.environment != null String resolvedLocation = (this.environment != null ?
? this.environment.resolveRequiredPlaceholders(location) : location); this.environment.resolveRequiredPlaceholders(location) : location);
return this.resourceLoader.getResource(resolvedLocation); return this.resourceLoader.getResource(resolvedLocation);
} }
catch (Exception ex) { catch (Exception ex) {
@ -598,15 +598,11 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
Map<String, String> mappings = buildImportAwareMappings(); Map<String, String> mappings = buildImportAwareMappings();
if (!mappings.isEmpty()) { if (!mappings.isEmpty()) {
GeneratedMethod generatedMethod = beanFactoryInitializationCode GeneratedMethod generatedMethod = beanFactoryInitializationCode.getMethods().add(
.getMethods() "addImportAwareBeanPostProcessors", method -> generateAddPostProcessorMethod(method, mappings));
.add("addImportAwareBeanPostProcessors", method -> beanFactoryInitializationCode.addInitializer(generatedMethod.toMethodReference());
generateAddPostProcessorMethod(method, mappings));
beanFactoryInitializationCode
.addInitializer(generatedMethod.toMethodReference());
ResourceHints hints = generationContext.getRuntimeHints().resources(); ResourceHints hints = generationContext.getRuntimeHints().resources();
mappings.forEach( mappings.forEach((target, from) -> hints.registerType(TypeReference.of(from)));
(target, from) -> hints.registerType(TypeReference.of(from)));
} }
} }
@ -635,8 +631,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
} }
private Map<String, String> buildImportAwareMappings() { private Map<String, String> buildImportAwareMappings() {
ImportRegistry importRegistry = this.beanFactory ImportRegistry importRegistry = this.beanFactory.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
.getBean(IMPORT_REGISTRY_BEAN_NAME, ImportRegistry.class);
Map<String, String> mappings = new LinkedHashMap<>(); Map<String, String> mappings = new LinkedHashMap<>();
for (String name : this.beanFactory.getBeanDefinitionNames()) { for (String name : this.beanFactory.getBeanDefinitionNames()) {
Class<?> beanType = this.beanFactory.getType(name); Class<?> beanType = this.beanFactory.getType(name);
@ -671,11 +666,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
@Override @Override
public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) { public void applyTo(GenerationContext generationContext, BeanFactoryInitializationCode beanFactoryInitializationCode) {
registerRuntimeHints(generationContext.getRuntimeHints()); registerRuntimeHints(generationContext.getRuntimeHints());
GeneratedMethod generatedMethod = beanFactoryInitializationCode GeneratedMethod generatedMethod = beanFactoryInitializationCode.getMethods()
.getMethods()
.add("processPropertySources", this::generateAddPropertySourceProcessorMethod); .add("processPropertySources", this::generateAddPropertySourceProcessorMethod);
beanFactoryInitializationCode beanFactoryInitializationCode.addInitializer(generatedMethod.toMethodReference());
.addInitializer(generatedMethod.toMethodReference());
} }
private void registerRuntimeHints(RuntimeHints hints) { private void registerRuntimeHints(RuntimeHints hints) {
@ -686,8 +679,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
} }
for (String location : descriptor.locations()) { for (String location : descriptor.locations()) {
Resource resource = this.resourceResolver.apply(location); Resource resource = this.resourceResolver.apply(location);
if (resource != null && resource.exists() && resource instanceof ClassPathResource classpathResource) { if (resource instanceof ClassPathResource classPathResource && classPathResource.exists()) {
hints.resources().registerPattern(classpathResource.getPath()); hints.resources().registerPattern(classPathResource.getPath());
} }
} }
} }
@ -723,8 +716,8 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
code.add("new $T(", PropertySourceDescriptor.class); code.add("new $T(", PropertySourceDescriptor.class);
CodeBlock values = descriptor.locations().stream() CodeBlock values = descriptor.locations().stream()
.map(value -> CodeBlock.of("$S", value)).collect(CodeBlock.joining(", ")); .map(value -> CodeBlock.of("$S", value)).collect(CodeBlock.joining(", "));
if (descriptor.name() == null && descriptor.propertySourceFactory() == null if (descriptor.name() == null && descriptor.propertySourceFactory() == null &&
&& descriptor.encoding() == null && !descriptor.ignoreResourceNotFound()) { descriptor.encoding() == null && !descriptor.ignoreResourceNotFound()) {
code.add("$L)", values); code.add("$L)", values);
} }
else { else {

Loading…
Cancel
Save