Skip to content

Commit 2669531

Browse files
committed
Avoid enhancement of configuration class in case of existing instance
Closes gh-25738
1 parent 889c4e0 commit 2669531

File tree

1 file changed

+19
-15
lines changed

1 file changed

+19
-15
lines changed

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

+19-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2024 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -507,14 +507,18 @@ public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFact
507507
throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" +
508508
beanName + "' since it is not stored in an AbstractBeanDefinition subclass");
509509
}
510-
else if (logger.isWarnEnabled() && beanFactory.containsSingleton(beanName)) {
511-
logger.warn("Cannot enhance @Configuration bean definition '" + beanName +
512-
"' since its singleton instance has been created too early. The typical cause " +
513-
"is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
514-
"return type: Consider declaring such methods as 'static' and/or marking the " +
515-
"containing configuration class as 'proxyBeanMethods=false'.");
510+
else if (beanFactory.containsSingleton(beanName)) {
511+
if (logger.isWarnEnabled()) {
512+
logger.warn("Cannot enhance @Configuration bean definition '" + beanName +
513+
"' since its singleton instance has been created too early. The typical cause " +
514+
"is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
515+
"return type: Consider declaring such methods as 'static' and/or marking the " +
516+
"containing configuration class as 'proxyBeanMethods=false'.");
517+
}
518+
}
519+
else {
520+
configBeanDefs.put(beanName, abd);
516521
}
517-
configBeanDefs.put(beanName, abd);
518522
}
519523
}
520524
if (configBeanDefs.isEmpty()) {
@@ -647,9 +651,9 @@ private Map<String, String> buildImportAwareMappings() {
647651
}
648652
return mappings;
649653
}
650-
651654
}
652655

656+
653657
private static class PropertySourcesAotContribution implements BeanFactoryInitializationAotContribution {
654658

655659
private static final String ENVIRONMENT_VARIABLE = "environment";
@@ -761,17 +765,18 @@ private CodeBlock handleNull(@Nullable Object value, Supplier<CodeBlock> nonNull
761765
return nonNull.get();
762766
}
763767
}
764-
765768
}
766769

770+
767771
private static class ConfigurationClassProxyBeanRegistrationCodeFragments extends BeanRegistrationCodeFragmentsDecorator {
768772

769773
private final RegisteredBean registeredBean;
770774

771775
private final Class<?> proxyClass;
772776

773-
public ConfigurationClassProxyBeanRegistrationCodeFragments(BeanRegistrationCodeFragments codeFragments,
774-
RegisteredBean registeredBean) {
777+
public ConfigurationClassProxyBeanRegistrationCodeFragments(
778+
BeanRegistrationCodeFragments codeFragments, RegisteredBean registeredBean) {
779+
775780
super(codeFragments);
776781
this.registeredBean = registeredBean;
777782
this.proxyClass = registeredBean.getBeanType().toClass();
@@ -780,6 +785,7 @@ public ConfigurationClassProxyBeanRegistrationCodeFragments(BeanRegistrationCode
780785
@Override
781786
public CodeBlock generateSetBeanDefinitionPropertiesCode(GenerationContext generationContext,
782787
BeanRegistrationCode beanRegistrationCode, RootBeanDefinition beanDefinition, Predicate<String> attributeFilter) {
788+
783789
CodeBlock.Builder code = CodeBlock.builder();
784790
code.add(super.generateSetBeanDefinitionPropertiesCode(generationContext,
785791
beanRegistrationCode, beanDefinition, attributeFilter));
@@ -790,8 +796,7 @@ public CodeBlock generateSetBeanDefinitionPropertiesCode(GenerationContext gener
790796

791797
@Override
792798
public CodeBlock generateInstanceSupplierCode(GenerationContext generationContext,
793-
BeanRegistrationCode beanRegistrationCode,
794-
boolean allowDirectSupplierShortcut) {
799+
BeanRegistrationCode beanRegistrationCode, boolean allowDirectSupplierShortcut) {
795800

796801
Executable executableToUse = proxyExecutable(generationContext.getRuntimeHints(),
797802
this.registeredBean.resolveConstructorOrFactoryMethod());
@@ -812,7 +817,6 @@ private Executable proxyExecutable(RuntimeHints runtimeHints, Executable userExe
812817
}
813818
return userExecutable;
814819
}
815-
816820
}
817821

818822
}

0 commit comments

Comments
 (0)