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

@Scope and other common bean annotations on nested @Component classes [SPR-12486] #17092

Closed
spring-projects-issues opened this issue Dec 1, 2014 · 10 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Dec 1, 2014

Dave Syer opened SPR-12486 and commented

A nested static class inside @Configuration is considered for inclusion in the bean factory, and if it is a @Component you get a bean definition, but the @Scope on the class is ignored. A bean that is created by a component scan does not behave this way (I presume), neither does it when added explicitly to the bean factory (e.g. in the constructor of AnnotationConfigApplicationContext).

I think #16497 refers obliquely to this problem. #13365 also looks relevant.


Affects: 4.1.2

Reference URL: spring-attic/spring-framework-issues#88

Issue Links:

Referenced from: commits c5c5473

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

N.B. I have some doubts whether @Scope could ever be valid on @Configuration (see for example spring-cloud/spring-cloud-config#43), but it does seem to make sense on a normal @Component.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

FWIW, a custom scope on a configuration class does work: It just means that for every invocation of a factory method on it, an instance of the configuration class is going to get obtained through that scope. Works quite naturally, even if it feels a little bit unusual. Note that beans defined by such a scoped configuration class will still be managed according to their own scope definition: a singleton bean defined by that configuration class will still be a singleton instance itself; it just happens to be created through the scoped config class.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

That's interesting because the problem reported above was easy to reproduce and the stack trace is quite intimidating. Does your assertion about custom scopes necessarily always hold? What if it's a scoped-proxy? (Maybe we are doing something wrong in Spring Cloud.)

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Hmm, I've added several unit tests for scoped configuration classes, and they all pass as expected... That said, good point about scoped proxies. I'll dig into that next.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Dec 1, 2014

Juergen Hoeller commented

Scoped proxies turn out to work fine for configuration classes as well - for regular setup scenarios at least.

I suspect the Spring Cloud issue above relates to #17050 due to some early initialization arrangements...

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Common annotation processing for imported and nested classes within configuration classes is in master now.

Please give the upcoming 4.1.3 snapshot a try, Dave... We're going to release 4.1.3 in time for Boot 1.2 GA still.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Hmm, both of those seem to be top-level configuration classes... It's unusual to have a scope on those, of course, but it should nevertheless work.

Anything obvious showing up when looking at the bean definition state in the debugger?

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

The beanClass is ScopedFactoryBean (not the proxy itself). Is that the problem?

Update: Actually that is expected. Still digging.

@spring-projects-issues
Copy link
Collaborator Author

Dave Syer commented

All working as expected now (once I got my expectations aligned with reality). Thanks.

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: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants