Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Spring tries to instantiate nested annotation with a @Component annotation [SPR-8761] #13404

Closed
spring-projects-issues opened this issue Oct 12, 2011 · 4 comments
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Stefan Gybas opened SPR-8761 and commented

When a component-scanned class has a nested annotation with a @Component annotation, Spring 3.1 RC1 tries to instantiate the nested annotation. For example:

// the test passes if you remove this annotation
@Component
public class WithNestedAnnotation {

    @Target({ ElementType.TYPE })
    @Retention(RetentionPolicy.RUNTIME)
    @Documented
    @Component
    public static @interface MyComponent {

        String value() default "";
    } 
}
@WithNestedAnnotation.MyComponent
public class UsesAnnotation {

}

fails with

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'com.example.WithNestedAnnotation$MyComponent#0': Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.example.WithNestedAnnotation$MyComponent]: Specified class is an interface
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:990)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:936)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:488)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:459)
	at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:103)
	at org.springframework.test.context.support.AbstractGenericContextLoader.loadContext(AbstractGenericContextLoader.java:1)
	at org.springframework.test.context.support.DelegatingSmartContextLoader.loadContext(DelegatingSmartContextLoader.java:228)
	at org.springframework.test.context.TestContext.loadApplicationContext(TestContext.java:124)
	at org.springframework.test.context.TestContext.getApplicationContext(TestContext.java:148)
	... 24 more
Caused by: org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [com.example.WithNestedAnnotation$MyComponent]: Specified class is an interface
	at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:56)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateBean(AbstractAutowireCapableBeanFactory.java:983)
	... 39 more

This works fine with Sping 3.0.6 so it's a regression in 3.1 RC1. When you move the @Component annotation from MyComponent to UsesAnnotation it also works with 3.1RC1.

Attached is a Maven/Eclipse project with a test that demonstrates the problem.


Affects: 3.1 M2, 3.1 RC1

Attachments:

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Hi Stefan,

Thanks for the reproduction project.

This is definitely a regression as you pointed out. It works against 3.1.0.M1, and begins failing with 3.1.0.M2. I think I know what it is; should have it wrapped up for RC2.

Thanks

Chris

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Note that I've added the test case to the issues repo at https://github.com/SpringSource/spring-framework-issues/tree/master/SPR-8761

@spring-projects-issues
Copy link
Collaborator Author

Chris Beams commented

Stefan, this is fixed. Please take a look and confirm if you can; we'll be releasing RC2 shortly.

@spring-projects-issues
Copy link
Collaborator Author

Stefan Gybas commented

Hi Chris,

I can confirm that it's fixed in 3.1 RC2. Thanks!

Stefan

@spring-projects-issues spring-projects-issues added type: regression A bug that is also a regression in: core Issues in core modules (aop, beans, core, context, expression) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.1 RC2 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: regression A bug that is also a regression
Projects
None yet
Development

No branches or pull requests

1 participant