Skip to content

Commit

Permalink
ConfigurationClassUtils should check bean methods in super classes
Browse files Browse the repository at this point in the history
  • Loading branch information
quaff committed May 10, 2023
1 parent 993a69d commit 572b62f
Showing 1 changed file with 18 additions and 6 deletions.
Expand Up @@ -48,6 +48,7 @@
* @author Juergen Hoeller
* @author Sam Brannen
* @author Stephane Nicoll
* @author Yanming Zhou
* @since 6.0
*/
public abstract class ConfigurationClassUtils {
Expand Down Expand Up @@ -136,7 +137,7 @@ else if (beanDef instanceof AbstractBeanDefinition abstractBd && abstractBd.hasB
if (config != null && !Boolean.FALSE.equals(config.get("proxyBeanMethods"))) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_FULL);
}
else if (config != null || isConfigurationCandidate(metadata)) {
else if (config != null || isConfigurationCandidate(metadata, metadataReaderFactory)) {
beanDef.setAttribute(CONFIGURATION_CLASS_ATTRIBUTE, CONFIGURATION_CLASS_LITE);
}
else {
Expand All @@ -156,10 +157,11 @@ else if (config != null || isConfigurationCandidate(metadata)) {
* Check the given metadata for a configuration class candidate
* (or nested component class declared within a configuration/component class).
* @param metadata the metadata of the annotated class
* @param metadataReaderFactory the current factory in use by the caller
* @return {@code true} if the given class is to be registered for
* configuration class processing; {@code false} otherwise
*/
static boolean isConfigurationCandidate(AnnotationMetadata metadata) {
static boolean isConfigurationCandidate(AnnotationMetadata metadata, MetadataReaderFactory metadataReaderFactory) {
// Do not consider an interface or an annotation...
if (metadata.isInterface()) {
return false;
Expand All @@ -173,19 +175,29 @@ static boolean isConfigurationCandidate(AnnotationMetadata metadata) {
}

// Finally, let's look for @Bean methods...
return hasBeanMethods(metadata);
return hasBeanMethods(metadata, metadataReaderFactory);
}

static boolean hasBeanMethods(AnnotationMetadata metadata) {
static boolean hasBeanMethods(AnnotationMetadata metadata, MetadataReaderFactory metadataReaderFactory) {
try {
return metadata.hasAnnotatedMethods(Bean.class.getName());
AnnotationMetadata md = metadata;
while (true) {
if (md.hasAnnotatedMethods(Bean.class.getName())) {
return true;
}
if (md.hasSuperClass()) {
md = metadataReaderFactory.getMetadataReader(md.getSuperClassName()).getAnnotationMetadata();
} else {
break;
}
}
}
catch (Throwable ex) {
if (logger.isDebugEnabled()) {
logger.debug("Failed to introspect @Bean methods on class [" + metadata.getClassName() + "]: " + ex);
}
return false;
}
return false;
}

/**
Expand Down

0 comments on commit 572b62f

Please sign in to comment.