|
|
|
|
@@ -114,6 +114,7 @@ import org.springframework.javapoet.ClassName;
|
|
|
|
|
import org.springframework.javapoet.CodeBlock;
|
|
|
|
|
import org.springframework.javapoet.CodeBlock.Builder;
|
|
|
|
|
import org.springframework.javapoet.MethodSpec;
|
|
|
|
|
import org.springframework.javapoet.NameAllocator;
|
|
|
|
|
import org.springframework.javapoet.ParameterizedTypeName;
|
|
|
|
|
import org.springframework.util.Assert;
|
|
|
|
|
import org.springframework.util.ClassUtils;
|
|
|
|
|
@@ -122,6 +123,7 @@ import org.springframework.util.LinkedMultiValueMap;
|
|
|
|
|
import org.springframework.util.MultiValueMap;
|
|
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
import org.springframework.util.ReflectionUtils;
|
|
|
|
|
import org.springframework.util.StringUtils;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* {@link BeanFactoryPostProcessor} used for bootstrapping processing of
|
|
|
|
|
@@ -197,7 +199,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
|
|
|
|
@SuppressWarnings("NullAway.Init")
|
|
|
|
|
private List<PropertySourceDescriptor> propertySourceDescriptors;
|
|
|
|
|
|
|
|
|
|
private Set<BeanRegistrar> beanRegistrars = new LinkedHashSet<>();
|
|
|
|
|
private Map<String, BeanRegistrar> beanRegistrars = new LinkedHashMap<>();
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
@@ -443,7 +445,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
|
|
|
|
}
|
|
|
|
|
this.reader.loadBeanDefinitions(configClasses);
|
|
|
|
|
for (ConfigurationClass configClass : configClasses) {
|
|
|
|
|
this.beanRegistrars.addAll(configClass.getBeanRegistrars());
|
|
|
|
|
this.beanRegistrars.putAll(configClass.getBeanRegistrars());
|
|
|
|
|
}
|
|
|
|
|
alreadyParsed.addAll(configClasses);
|
|
|
|
|
processConfig.tag("classCount", () -> String.valueOf(configClasses.size())).end();
|
|
|
|
|
@@ -846,13 +848,13 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
|
|
|
|
|
|
|
|
|
private static final String ENVIRONMENT_VARIABLE = "environment";
|
|
|
|
|
|
|
|
|
|
private final Set<BeanRegistrar> beanRegistrars;
|
|
|
|
|
private final Map<String, BeanRegistrar> beanRegistrars;
|
|
|
|
|
|
|
|
|
|
private final ConfigurableListableBeanFactory beanFactory;
|
|
|
|
|
|
|
|
|
|
private final AotServices<BeanRegistrationAotProcessor> aotProcessors;
|
|
|
|
|
|
|
|
|
|
public BeanRegistrarAotContribution(Set<BeanRegistrar> beanRegistrars, ConfigurableListableBeanFactory beanFactory) {
|
|
|
|
|
public BeanRegistrarAotContribution(Map<String, BeanRegistrar> beanRegistrars, ConfigurableListableBeanFactory beanFactory) {
|
|
|
|
|
this.beanRegistrars = beanRegistrars;
|
|
|
|
|
this.beanFactory = beanFactory;
|
|
|
|
|
this.aotProcessors = AotServices.factoriesAndBeans(this.beanFactory).load(BeanRegistrationAotProcessor.class);
|
|
|
|
|
@@ -935,13 +937,32 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
|
|
|
|
|
|
|
|
|
private CodeBlock generateRegisterCode() {
|
|
|
|
|
Builder code = CodeBlock.builder();
|
|
|
|
|
for (BeanRegistrar beanRegistrar : this.beanRegistrars) {
|
|
|
|
|
code.addStatement("new $T().register(new $T(($T)$L, $L, $L, $T.class, $L), $L)", beanRegistrar.getClass(),
|
|
|
|
|
Builder metadataReaderFactoryCode = null;
|
|
|
|
|
NameAllocator nameAllocator = new NameAllocator();
|
|
|
|
|
for (Map.Entry<String, BeanRegistrar> beanRegistrarEntry : this.beanRegistrars.entrySet()) {
|
|
|
|
|
BeanRegistrar beanRegistrar = beanRegistrarEntry.getValue();
|
|
|
|
|
String beanRegistrarName = nameAllocator.newName(StringUtils.uncapitalize(beanRegistrar.getClass().getSimpleName()));
|
|
|
|
|
code.addStatement("$T $L = new $T()", beanRegistrar.getClass(), beanRegistrarName, beanRegistrar.getClass());
|
|
|
|
|
if (beanRegistrar instanceof ImportAware) {
|
|
|
|
|
if (metadataReaderFactoryCode == null) {
|
|
|
|
|
metadataReaderFactoryCode = CodeBlock.builder();
|
|
|
|
|
metadataReaderFactoryCode.addStatement("$T metadataReaderFactory = new $T()",
|
|
|
|
|
MetadataReaderFactory.class, CachingMetadataReaderFactory.class);
|
|
|
|
|
}
|
|
|
|
|
code.beginControlFlow("try")
|
|
|
|
|
.addStatement("$L.setImportMetadata(metadataReaderFactory.getMetadataReader($S).getAnnotationMetadata())",
|
|
|
|
|
beanRegistrarName, beanRegistrarEntry.getKey())
|
|
|
|
|
.nextControlFlow("catch ($T ex)", IOException.class)
|
|
|
|
|
.addStatement("throw new $T(\"Failed to read metadata for '$L'\", ex)",
|
|
|
|
|
IllegalStateException.class, beanRegistrarEntry.getKey())
|
|
|
|
|
.endControlFlow();
|
|
|
|
|
}
|
|
|
|
|
code.addStatement("$L.register(new $T(($T)$L, $L, $L, $T.class, $L), $L)", beanRegistrarName,
|
|
|
|
|
BeanRegistryAdapter.class, BeanDefinitionRegistry.class, BeanFactoryInitializationCode.BEAN_FACTORY_VARIABLE,
|
|
|
|
|
BeanFactoryInitializationCode.BEAN_FACTORY_VARIABLE, ENVIRONMENT_VARIABLE, beanRegistrar.getClass(),
|
|
|
|
|
CUSTOMIZER_MAP_VARIABLE, ENVIRONMENT_VARIABLE);
|
|
|
|
|
}
|
|
|
|
|
return code.build();
|
|
|
|
|
return (metadataReaderFactoryCode == null ? code.build() : metadataReaderFactoryCode.add(code.build()).build());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private CodeBlock generateInitDestroyMethods(String beanName, AbstractBeanDefinition beanDefinition,
|
|
|
|
|
|