Warn about non-static BeanDefinitionRegistryPostProcessor declarations on @Configuration classes

Issue: SPR-14234
(cherry picked from commit 7737c3c)
This commit is contained in:
Juergen Hoeller 2016-05-02 15:19:05 +02:00
parent 85675fbe2e
commit 0589c1b4b5
2 changed files with 10 additions and 3 deletions

View File

@ -159,7 +159,7 @@ class ConfigurationClassEnhancer {
* Conditional {@link Callback}.
* @see ConditionalCallbackFilter
*/
private static interface ConditionalCallback extends Callback {
private interface ConditionalCallback extends Callback {
boolean isMatch(Method candidateMethod);
}
@ -343,7 +343,8 @@ class ConfigurationClassEnhancer {
// The factory is calling the bean method in order to instantiate and register the bean
// (i.e. via a getBean() call) -> invoke the super implementation of the method to actually
// create the bean instance.
if (BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) {
if (logger.isWarnEnabled() &&
BeanFactoryPostProcessor.class.isAssignableFrom(beanMethod.getReturnType())) {
logger.warn(String.format("@Bean method %s.%s is non-static and returns an object " +
"assignable to Spring's BeanFactoryPostProcessor interface. This will " +
"result in a failure to process annotations such as @Autowired, " +

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2015 the original author or authors.
* Copyright 2002-2016 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.
@ -382,6 +382,12 @@ 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'.");
}
configBeanDefs.put(beanName, (AbstractBeanDefinition) beanDef);
}
}