Skip to content

AnnotationBeanNameGenerator fails when an annotation references a non-existent class #36524

Closed

Description

@Chr3is

I have an AutoConfiguration, which uses @AutoConfigureBefore. The documentation states, that it's safe to specify classes that may not be on the classpath. However, a ClassNotFoundException is thrown if the auto configuration is included in a module, which does not have the class in the classpath:

@AutoConfiguration
@AutoConfigureBefore(LiquibaseAutoConfiguration.class)
public class AutoConfig {

}
Caused by: java.lang.IllegalStateException: Failed to generate bean name for imported class 'com.example.AutoConfig'
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.registerBeanDefinitionForImportedConfigurationClass(ConfigurationClassBeanDefinitionReader.java:172)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForConfigurationClass(ConfigurationClassBeanDefinitionReader.java:145)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.loadBeanDefinitions(ConfigurationClassBeanDefinitionReader.java:124)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:464)
	at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:316)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:349)
	at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:118)
	at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:795)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:603)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:756)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:445)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:321)
	at org.springframework.boot.test.context.SpringBootContextLoader.lambda$loadContext$2(SpringBootContextLoader.java:155)
	at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:58)
	at org.springframework.util.function.ThrowingSupplier.get(ThrowingSupplier.java:46)
	at org.springframework.boot.SpringApplication.withHook(SpringApplication.java:1465)
	at org.springframework.boot.test.context.SpringBootContextLoader$ContextLoaderHook.run(SpringBootContextLoader.java:600)
	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:155)
	at org.springframework.boot.test.context.SpringBootContextLoader.loadContext(SpringBootContextLoader.java:114)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContextInternal(DefaultCacheAwareContextLoaderDelegate.java:247)
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.lambda$loadContext$0(DefaultCacheAwareContextLoaderDelegate.java:167)
	... 21 more
Caused by: java.lang.IllegalArgumentException: Could not find class [org.springframework.boot.liquibase.autoconfigure.LiquibaseAutoConfiguration]
	at org.springframework.util.ClassUtils.resolveClassName(ClassUtils.java:353)
	at org.springframework.core.annotation.TypeMappedAnnotation.adapt(TypeMappedAnnotation.java:451)
	at org.springframework.core.annotation.TypeMappedAnnotation.getValue(TypeMappedAnnotation.java:384)
	at org.springframework.core.annotation.TypeMappedAnnotation.asMap(TypeMappedAnnotation.java:273)
	at org.springframework.core.annotation.AbstractMergedAnnotation.asAnnotationAttributes(AbstractMergedAnnotation.java:191)
	at org.springframework.context.annotation.AnnotationBeanNameGenerator.determineBeanNameFromAnnotation(AnnotationBeanNameGenerator.java:143)
	at org.springframework.context.annotation.AnnotationBeanNameGenerator.generateBeanName(AnnotationBeanNameGenerator.java:110)
	at org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.registerBeanDefinitionForImportedConfigurationClass(ConfigurationClassBeanDefinitionReader.java:168)

This issue appeared during the spring boot 3.5 -> 4.0.x migration.

I provided a simple example to demonstrate this issue.

demo 9.zip

Maybe related: #35631

Activity

  1. added
    status: waiting-for-triage
    on Mar 23, 2026
  2. wilkinsona commented on Mar 23, 2026

    @wilkinsona
    Member

    Thanks for the report.

    I think the javadoc's inaccurate, although I believe that's the case in Spring Boot 3.5.x as well as 4.0.x. Your report suggests that you've seen a change in behavior when upgrading from 3.5 to 4.0 so we don't seem to have the complete picture here.

    Please provide a minimal sample that uses Spring Boot 3.5 and does not fail when @AutoConfigureBefore or @AutoConfigureAfter references a class that is not on the classpath.

  3. added
    status: waiting-for-feedback
    on Mar 23, 2026
  4. Chr3is commented on Mar 23, 2026

    @Chr3is
    Author

    @wilkinsona I have to check this. The issue might not happen in this case with spring boot 3.5 because the used auto config classes e.g. LiquibaseAutoConfiguration in the annotations were always present in the classpath due to spring-boot-autoconfigure, which was modularized with spring boot 4.

  5. added
    status: feedback-provided
    and removed
    status: waiting-for-feedback
    on Mar 23, 2026
  6. wilkinsona commented on Mar 23, 2026

    @wilkinsona
    Member

    I think that's more than likely to be the case. It certainly is in my local experimentation. We can update the javadoc in 3.5.x and later.

    As part of those updates, we should review all of the changes made for spring-projects/spring-boot#48395. I think the new guidance only holds true for conditions and only for conditions that only match when all the referenced classes are present. If an annotated class can remain in use when a referenced class is absent, it needs to use a class name in the annotation to reference the class otherwise something introspecting the class's annotations will fail.

    /cc @sbrannen

  7. changed the title [-]ClassNotFoundException with @AutoConfigureBefore or @AutoConfigureAfter[/-] [+]Guidance for when to use class names and when to use class references is inaccurate[/+] on Mar 23, 2026
  8. added
    type: documentation
    and removed
    status: waiting-for-triage
    status: feedback-provided
    on Mar 23, 2026
  9. Chr3is commented on Mar 23, 2026

    @Chr3is
    Author

    Using

    @AutoConfiguration("MyAutoConfig") // <-- set bean name explicitly
    @AutoConfigureBefore(LiquibaseAutoConfiguration.class)
    public class AutoConfig {
    
    }

    would work as well.
    It would be nice if spring would not fail due to a bean name generation rather falling back to a random unique name. However, I don't know the cases, where spring tries to load the class for which purpose.

  10. wilkinsona commented on Mar 23, 2026

    @wilkinsona
    Member

    That wouldn't help as the problem's broader than just bean name generation. The @AutoConfigureBefore that references a non-existent class will hinder introspection of any of the class's annotations using any of Java's standard mechanisms.

  11. changed the title [-]Guidance for when to use class names and when to use class references is inaccurate[/-] [+]MergedAnnotation.asAnnotationAttributes(Adapt...) fails when an annotation attribute references a non-existent class[/+] on Mar 24, 2026
  12. 4 remaining items

  13. added
    in: core
    on Mar 25, 2026
  14. added
    type: regression
    and removed
    status: waiting-for-triage
    on Apr 1, 2026
  15. added this to the 7.0.7 milestone on Apr 1, 2026
  16. changed the title [-]MergedAnnotation.asAnnotationAttributes(Adapt...) fails when an annotation attribute references a non-existent class[/-] [+]`MergedAnnotation.asAnnotationAttributes()` fails when an attribute references a non-existent class[/+] on Apr 1, 2026
  17. sbrannen commented on Apr 1, 2026

    @sbrannen
    Member

    Thanks for the report!

    This one turns out to be a bit involved.

    We've had support for lazily propagating such exceptions in our annotation processing for a long time. Specifically, we used to catch and store the exception in the annotation attributes map and then throw an exception only if the user actually attempted to access that particular attribute.

    In fact, AnnotationAttributes.getRequiredAttribute() still contains the logic for the latter part.

    However, when the MergedAnnotation infrastructure was introduced in Spring Framework 5.2, the code that stored the exception in the attributes map got accidentally removed.

    Thus, the underlying bug is effectively a regression that was introduced in Spring Framework 5.2.

    The reason this was first noticed in the migration from Spring Boot 3.5 to 4.0.x is that I updated the AnnotationBeanNameGenerator to make direct use of the MergedAnnotations APIs in Spring Framework 6.1. In commit a199654, we started using MergedAnnotation.asAnnotationAttributes() which made that old regression surface.

    Specifically, MergedAnnotation.asAnnotationAttributes() eagerly throws an exception for an annotation whose attribute references a type not present on the classpath; whereas, our annotation processing used to catch and store that exception, as mentioned above.

    Thus, we plan to patch AnnotationBeanNameGenerator in Spring Framework 6.2.x to avoid the issue and then provide a more robust solution for MergedAnnotation.asAnnotationAttributes() and MergedAnnotation.asMap() in Spring Framework 7.0.x.

    Related Issues

  18. changed the title [-]`MergedAnnotation.asAnnotationAttributes()` fails when an attribute references a non-existent class[/-] [+]`AnnotationBeanNameGenerator` fails when an annotation references a non-existent class[/+] on Apr 2, 2026
  19. added a commit that references this issue on Apr 2, 2026
    00fbd91
  20. added a commit that references this issue on Apr 2, 2026
    d78d80b
  21. added
    status: backported
    on Apr 2, 2026
  22. sbrannen commented on Apr 2, 2026

    @sbrannen
    Member

    AnnotationBeanNameGenerator has been patched for 7.0.7, and the patch has been backported to 6.2.18.

    Further work will be performed in conjunction with #36586.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

in: corestatus: backportedtype: regression

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions