Avoid enhancement of configuration class in case of existing instance
Closes gh-25738
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2023 the original author or authors.
|
||||
* Copyright 2002-2024 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -507,14 +507,18 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" +
|
||||
beanName + "' since it is not stored in an AbstractBeanDefinition subclass");
|
||||
}
|
||||
else if (logger.isWarnEnabled() && beanFactory.containsSingleton(beanName)) {
|
||||
logger.warn("Cannot enhance @Configuration bean definition '" + beanName +
|
||||
"' since its singleton instance has been created too early. The typical cause " +
|
||||
"is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
|
||||
"return type: Consider declaring such methods as 'static' and/or marking the " +
|
||||
"containing configuration class as 'proxyBeanMethods=false'.");
|
||||
else if (beanFactory.containsSingleton(beanName)) {
|
||||
if (logger.isWarnEnabled()) {
|
||||
logger.warn("Cannot enhance @Configuration bean definition '" + beanName +
|
||||
"' since its singleton instance has been created too early. The typical cause " +
|
||||
"is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
|
||||
"return type: Consider declaring such methods as 'static' and/or marking the " +
|
||||
"containing configuration class as 'proxyBeanMethods=false'.");
|
||||
}
|
||||
}
|
||||
else {
|
||||
configBeanDefs.put(beanName, abd);
|
||||
}
|
||||
configBeanDefs.put(beanName, abd);
|
||||
}
|
||||
}
|
||||
if (configBeanDefs.isEmpty()) {
|
||||
@@ -647,9 +651,9 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
}
|
||||
return mappings;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class PropertySourcesAotContribution implements BeanFactoryInitializationAotContribution {
|
||||
|
||||
private static final String ENVIRONMENT_VARIABLE = "environment";
|
||||
@@ -761,17 +765,18 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
return nonNull.get();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
private static class ConfigurationClassProxyBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
|
||||
|
||||
private final RegisteredBean registeredBean;
|
||||
|
||||
private final Class<?> proxyClass;
|
||||
|
||||
public ConfigurationClassProxyBeanRegistrationCodeFragments(BeanRegistrationCodeFragments codeFragments,
|
||||
RegisteredBean registeredBean) {
|
||||
public ConfigurationClassProxyBeanRegistrationCodeFragments(
|
||||
BeanRegistrationCodeFragments codeFragments, RegisteredBean registeredBean) {
|
||||
|
||||
super(codeFragments);
|
||||
this.registeredBean = registeredBean;
|
||||
this.proxyClass = registeredBean.getBeanType().toClass();
|
||||
@@ -780,6 +785,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
@Override
|
||||
public CodeBlock generateSetBeanDefinitionPropertiesCode(GenerationContext generationContext,
|
||||
BeanRegistrationCode beanRegistrationCode, RootBeanDefinition beanDefinition, Predicate<String> attributeFilter) {
|
||||
|
||||
CodeBlock.Builder code = CodeBlock.builder();
|
||||
code.add(super.generateSetBeanDefinitionPropertiesCode(generationContext,
|
||||
beanRegistrationCode, beanDefinition, attributeFilter));
|
||||
@@ -790,8 +796,7 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
|
||||
@Override
|
||||
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext,
|
||||
BeanRegistrationCode beanRegistrationCode,
|
||||
boolean allowDirectSupplierShortcut) {
|
||||
BeanRegistrationCode beanRegistrationCode, boolean allowDirectSupplierShortcut) {
|
||||
|
||||
Executable executableToUse = proxyExecutable(generationContext.getRuntimeHints(),
|
||||
this.registeredBean.resolveConstructorOrFactoryMethod());
|
||||
@@ -812,7 +817,6 @@ public class ConfigurationClassPostProcessor implements BeanDefinitionRegistryPo
|
||||
}
|
||||
return userExecutable;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user