Skip to content

Unresolvable class in 'basePackageClasses' annotation attribute results in misleading and hard to diagnose error [SPR-13177] #17769

@spring-projects-issues

Description

@spring-projects-issues

Tim Reidel opened SPR-13177 and commented

I ran into an issue with my spring-based project that results in a hard-to diagnose error. Fortunately I also have the solution. :)

When starting my application using spring-boot I get the following exception trace:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [ XXXXXXX ]; nested exception is java.lang.IllegalArgumentException: Attribute 'basePackageClasses' not found
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:179)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:306)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:239)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:606)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:462)
        at org.springframework.boot.context.embedded.EmbeddedWebApplicationContext.refresh(EmbeddedWebApplicationContext.java:109)
        at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:691)
        at org.springframework.boot.SpringApplication.run(SpringApplication.java:320)
        at com.fancypants.common.application.Application.main(Application.java:36)
Caused by: java.lang.IllegalArgumentException: Attribute 'basePackageClasses' not found
        at org.springframework.util.Assert.notNull(Assert.java:112)
        at org.springframework.core.annotation.AnnotationAttributes.doGet(AnnotationAttributes.java:108)
        at org.springframework.core.annotation.AnnotationAttributes.getClassArray(AnnotationAttributes.java:93)
        at org.springframework.context.annotation.ComponentScanAnnotationParser.parse(ComponentScanAnnotationParser.java:127)
        at org.springframework.context.annotation.ConfigurationClassParser.doProcessConfigurationClass(ConfigurationClassParser.java:265)
        at org.springframework.context.annotation.ConfigurationClassParser.processConfigurationClass(ConfigurationClassParser.java:229)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:196)
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:165)
        ... 10 more

where XXXXXXX = one of my configuration classes.

basePackageClasses is certainly defined in the @ComponentScan annotation. It turns out that one of the classes referenced in basePackageClasses is not defined. The effect of this is that in AnnotationReadingVisitorUtils (which helps to parse the annotation) a ClassNotFoundException is thrown. This is expected and normal since my configuration was wrong and I was missing some jars in the classpath.

The ClassNotFoundException is caught by this piece of code in AnnotationReadingVisitorUtils:

catch (Exception ex) {
	// Class not found - can't resolve class reference in annotation
	// attribute.
}

Nothing is done with the error with the effect being that basePackageClasses is not defined in the resulting AnnotiationAttributes object. Later on when the AnnotationAttributes object is queried for the basePackageClasses they aren't found, resulting in the misleading error above.

My suggestion is to throw an IllegalArgumentException in the catch block above, indicating which class was not found. I can likely submit a pull request to this effect if needed.


Affects: 4.1.6

Referenced from: commits b7acddb

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions