Skip to content

Commit 0589c1b

Browse files
committed
Warn about non-static BeanDefinitionRegistryPostProcessor declarations on @configuration classes
Issue: SPR-14234 (cherry picked from commit 7737c3c)
1 parent 85675fb commit 0589c1b

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public interface EnhancedConfiguration extends BeanFactoryAware {
159159
* Conditional {@link Callback}.
160160
* @see ConditionalCallbackFilter
161161
*/
162-
private static interface ConditionalCallback extends Callback {
162+
private interface ConditionalCallback extends Callback {
163163

164164
boolean isMatch(Method candidateMethod);
165165
}
@@ -343,7 +343,8 @@ public Object intercept(Object enhancedConfigInstance, Method beanMethod, Object
343343
// The factory is calling the bean method in order to instantiate and register the bean
344344
// (i.e. via a getBean() call) -> invoke the super implementation of the method to actually
345345
// create the bean instance.
346-
if (BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) {
346+
if (logger.isWarnEnabled() &&
347+
BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) {
347348
logger.warn(String.format("@Bean method %s.%s is non-static and returns an object " +
348349
"assignable to Spring's BeanFactoryPostProcessor interface. This will " +
349350
"result in a failure to process annotations such as @Autowired, " +

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2016 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.
@@ -382,6 +382,12 @@ public void enhanceConfigurationClasses(ConfigurableListableBeanFactory beanFact
382382
throw new BeanDefinitionStoreException("Cannot enhance @Configuration bean definition '" +
383383
beanName + "' since it is not stored in an AbstractBeanDefinition subclass");
384384
}
385+
else if (logger.isWarnEnabled() && beanFactory.containsSingleton(beanName)) {
386+
logger.warn("Cannot enhance @Configuration bean definition '" + beanName +
387+
"' since its singleton instance has been created too early. The typical cause " +
388+
"is a non-static @Bean method with a BeanDefinitionRegistryPostProcessor " +
389+
"return type: Consider declaring such methods as 'static'.");
390+
}
385391
configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef);
386392
}
387393
}

0 commit comments

Comments
 (0)