|
114 | 114 | import org.springframework.javapoet.CodeBlock;
|
115 | 115 | import org.springframework.javapoet.CodeBlock.Builder;
|
116 | 116 | import org.springframework.javapoet.MethodSpec;
|
| 117 | +import org.springframework.javapoet.NameAllocator; |
117 | 118 | import org.springframework.javapoet.ParameterizedTypeName;
|
118 | 119 | import org.springframework.util.Assert;
|
119 | 120 | import org.springframework.util.ClassUtils;
|
|
122 | 123 | import org.springframework.util.MultiValueMap;
|
123 | 124 | import org.springframework.util.ObjectUtils;
|
124 | 125 | import org.springframework.util.ReflectionUtils;
|
| 126 | +import org.springframework.util.StringUtils; |
125 | 127 |
|
126 | 128 | /**
|
127 | 129 | * {@link BeanFactoryPostProcessor} used for bootstrapping processing of
|
@@ -197,7 +199,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
197 | 199 | @SuppressWarnings("NullAway.Init")
|
198 | 200 | private List<PropertySourceDescriptor> propertySourceDescriptors;
|
199 | 201 |
|
200 |
| - private Set<BeanRegistrar> beanRegistrars = new LinkedHashSet<>(); |
| 202 | + private Map<String, BeanRegistrar> beanRegistrars = new LinkedHashMap<>(); |
201 | 203 |
|
202 | 204 |
|
203 | 205 | @Override
|
@@ -443,7 +445,7 @@ else if (ConfigurationClassUtils.checkConfigurationClassCandidate(beanDef, this.
|
443 | 445 | }
|
444 | 446 | this.reader.loadBeanDefinitions(configClasses);
|
445 | 447 | for (ConfigurationClass configClass : configClasses) {
|
446 |
| - this.beanRegistrars.addAll(configClass.getBeanRegistrars()); |
| 448 | + this.beanRegistrars.putAll(configClass.getBeanRegistrars()); |
447 | 449 | }
|
448 | 450 | alreadyParsed.addAll(configClasses);
|
449 | 451 | processConfig.tag("classCount", () -> String.valueOf(configClasses.size())).end();
|
@@ -846,13 +848,13 @@ private static class BeanRegistrarAotContribution implements BeanFactoryInitiali
|
846 | 848 |
|
847 | 849 | private static final String ENVIRONMENT_VARIABLE = "environment";
|
848 | 850 |
|
849 |
| - private final Set<BeanRegistrar> beanRegistrars; |
| 851 | + private final Map<String, BeanRegistrar> beanRegistrars; |
850 | 852 |
|
851 | 853 | private final ConfigurableListableBeanFactory beanFactory;
|
852 | 854 |
|
853 | 855 | private final AotServices<BeanRegistrationAotProcessor> aotProcessors;
|
854 | 856 |
|
855 |
| - public BeanRegistrarAotContribution(Set<BeanRegistrar> beanRegistrars, ConfigurableListableBeanFactory beanFactory) { |
| 857 | + public BeanRegistrarAotContribution(Map<String, BeanRegistrar> beanRegistrars, ConfigurableListableBeanFactory beanFactory) { |
856 | 858 | this.beanRegistrars = beanRegistrars;
|
857 | 859 | this.beanFactory = beanFactory;
|
858 | 860 | this.aotProcessors = AotServices.factoriesAndBeans(this.beanFactory).load(BeanRegistrationAotProcessor.class);
|
@@ -935,13 +937,32 @@ private CodeBlock generateCustomizerMap() {
|
935 | 937 |
|
936 | 938 | private CodeBlock generateRegisterCode() {
|
937 | 939 | Builder code = CodeBlock.builder();
|
938 |
| - for (BeanRegistrar beanRegistrar : this.beanRegistrars) { |
939 |
| - code.addStatement("new $T().register(new $T(($T)$L, $L, $L, $T.class, $L), $L)", beanRegistrar.getClass(), |
| 940 | + Builder metadataReaderFactoryCode = null; |
| 941 | + NameAllocator nameAllocator = new NameAllocator(); |
| 942 | + for (Map.Entry<String, BeanRegistrar> beanRegistrarEntry : this.beanRegistrars.entrySet()) { |
| 943 | + BeanRegistrar beanRegistrar = beanRegistrarEntry.getValue(); |
| 944 | + String beanRegistrarName = nameAllocator.newName(StringUtils.uncapitalize(beanRegistrar.getClass().getSimpleName())); |
| 945 | + code.addStatement("$T $L = new $T()", beanRegistrar.getClass(), beanRegistrarName, beanRegistrar.getClass()); |
| 946 | + if (beanRegistrar instanceof ImportAware) { |
| 947 | + if (metadataReaderFactoryCode == null) { |
| 948 | + metadataReaderFactoryCode = CodeBlock.builder(); |
| 949 | + metadataReaderFactoryCode.addStatement("$T metadataReaderFactory = new $T()", |
| 950 | + MetadataReaderFactory.class, CachingMetadataReaderFactory.class); |
| 951 | + } |
| 952 | + code.beginControlFlow("try") |
| 953 | + .addStatement("$L.setImportMetadata(metadataReaderFactory.getMetadataReader($S).getAnnotationMetadata())", |
| 954 | + beanRegistrarName, beanRegistrarEntry.getKey()) |
| 955 | + .nextControlFlow("catch ($T ex)", IOException.class) |
| 956 | + .addStatement("throw new $T(\"Failed to read metadata for '$L'\", ex)", |
| 957 | + IllegalStateException.class, beanRegistrarEntry.getKey()) |
| 958 | + .endControlFlow(); |
| 959 | + } |
| 960 | + code.addStatement("$L.register(new $T(($T)$L, $L, $L, $T.class, $L), $L)", beanRegistrarName, |
940 | 961 | BeanRegistryAdapter.class, BeanDefinitionRegistry.class, BeanFactoryInitializationCode.BEAN_FACTORY_VARIABLE,
|
941 | 962 | BeanFactoryInitializationCode.BEAN_FACTORY_VARIABLE, ENVIRONMENT_VARIABLE, beanRegistrar.getClass(),
|
942 | 963 | CUSTOMIZER_MAP_VARIABLE, ENVIRONMENT_VARIABLE);
|
943 | 964 | }
|
944 |
| - return code.build(); |
| 965 | + return (metadataReaderFactoryCode == null ? code.build() : metadataReaderFactoryCode.add(code.build()).build()); |
945 | 966 | }
|
946 | 967 |
|
947 | 968 | private CodeBlock generateInitDestroyMethods(String beanName, AbstractBeanDefinition beanDefinition,
|
|
0 commit comments