Allow AOT contributions to customize code fragments.

Update the `BeanRegistrationAotContribution` interface to allow
it to customize `BeanRegistrationCodeFragments`. This change
allows us to drop the `BeanRegistrationCodeFragmentsCustomizer`
interface since an `BeanRegistrationAotProcessor` can now be
used instead.

Closes gh-28557
This commit is contained in:
Phillip Webb
2022-06-02 14:51:22 -07:00
parent 74caa9213a
commit 8d79ec0b67
11 changed files with 143 additions and 156 deletions

View File

@@ -26,9 +26,10 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.aot.generate.GeneratedMethod;
import org.springframework.aot.generate.GenerationContext;
import org.springframework.beans.factory.aot.BeanRegistrationAotContribution;
import org.springframework.beans.factory.aot.BeanRegistrationAotProcessor;
import org.springframework.beans.factory.aot.BeanRegistrationCode;
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragments;
import org.springframework.beans.factory.aot.BeanRegistrationCodeFragmentsCustomizer;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.beans.factory.support.InstanceSupplier;
@@ -39,39 +40,37 @@ import org.springframework.javapoet.CodeBlock;
import org.springframework.lang.Nullable;
/**
* {@link BeanRegistrationCodeFragmentsCustomizer} for
* {@link ScopedProxyFactoryBean}.
* {@link BeanRegistrationAotProcessor} for {@link ScopedProxyFactoryBean}.
*
* @author Stephane Nicoll
* @author Phillip Webb
* @since 6.0
*/
class ScopedProxyBeanRegistrationCodeFragmentsCustomizer
implements BeanRegistrationCodeFragmentsCustomizer {
class ScopedProxyBeanRegistrationAotProcessor
implements BeanRegistrationAotProcessor {
private static final Log logger = LogFactory
.getLog(ScopedProxyBeanRegistrationCodeFragmentsCustomizer.class);
.getLog(ScopedProxyBeanRegistrationAotProcessor.class);
@Override
public BeanRegistrationCodeFragments customizeBeanRegistrationCodeFragments(
RegisteredBean registeredBean, BeanRegistrationCodeFragments codeFragments) {
public BeanRegistrationAotContribution processAheadOfTime(RegisteredBean registeredBean) {
Class<?> beanType = registeredBean.getBeanType().toClass();
if (!beanType.equals(ScopedProxyFactoryBean.class)) {
return codeFragments;
if (beanType.equals(ScopedProxyFactoryBean.class)) {
String targetBeanName = getTargetBeanName(
registeredBean.getMergedBeanDefinition());
BeanDefinition targetBeanDefinition = getTargetBeanDefinition(
registeredBean.getBeanFactory(), targetBeanName);
if (targetBeanDefinition == null) {
logger.warn("Could not handle " + ScopedProxyFactoryBean.class.getSimpleName()
+ ": no target bean definition found with name " + targetBeanName);
return null;
}
return BeanRegistrationAotContribution.ofBeanRegistrationCodeFragmentsCustomizer(codeFragments ->
new ScopedProxyBeanRegistrationCodeFragments(codeFragments, registeredBean,
targetBeanName, targetBeanDefinition));
}
String targetBeanName = getTargetBeanName(
registeredBean.getMergedBeanDefinition());
BeanDefinition targetBeanDefinition = getTargetBeanDefinition(
registeredBean.getBeanFactory(), targetBeanName);
if (targetBeanDefinition == null) {
logger.warn("Could not handle " + ScopedProxyFactoryBean.class.getSimpleName()
+ ": no target bean definition found with name " + targetBeanName);
return codeFragments;
}
return new ScopedProxyBeanRegistrationCodeFragments(codeFragments, registeredBean,
targetBeanName, targetBeanDefinition);
return null;
}
@Nullable