Skip to content

Support custom base scope annotation type for @Bean methods [SPR-13280] #17870

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

Closed
spring-projects-issues opened this issue Jul 25, 2015 · 6 comments
Assignees
Labels
in: core Issues in core modules (aop, beans, core, context, expression) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement

Comments

@spring-projects-issues
Copy link
Collaborator

spring-projects-issues commented Jul 25, 2015

Sam Brannen opened SPR-13280 and commented

Status Quo

AnnotationScopeMetadataResolver supports custom scope annotation types via its scopeAnnotationType property, and ConfigurationClassBeanDefinitionReader internally uses an AnnotationScopeMetadataResolver for certain uses cases (see its scopeMetadataResolver field).

However, ConfigurationClassBeanDefinitionReader does not consistently support custom scope annotation types for all use cases. For example, ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod() hard codes the scope annotation type to org.springframework.context.annotation.Scope.


Deliverables

  1. Determine why the scope annotation type in ConfigurationClassBeanDefinitionReader.loadBeanDefinitionsForBeanMethod() is hard coded to org.springframework.context.annotation.Scope.
    • See TODO in ConfigurationClassBeanDefinitionReader.
  2. Support custom scope annotation types consistently throughout ConfigurationClassBeanDefinitionReader.

Affects: 4.0 GA

Issue Links:

Referenced from: commits f0ac278, aae0bd2

1 votes, 4 watchers

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

This is actually intentional: ScopeMetadataResolver is designed to work at the type level only, so we're not using it to determine method-level scope declarations.

We could open this up to support method-level annotations, I suppose, but then we'd also have to pass a custom ScopeMetadataResolver into ConfigurationClassPostProcessor and co. Note that we're not doing this for imported configuration classes either; we're using a hard-coded AnnotationScopeMetadataResolver there.

Overall, the use of completely custom scope annotations isn't very compelling with configuration classes where plenty of Spring-specific annotations have to be used anyway. It should be good enough to allow for composable scope annotations, meta-annotated with Spring's @Scope, which we do already.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

Sam Brannen commented

I was mainly just curious about why the difference existed (and I admittedly did not investigate it fully on my own), but now it makes sense.

Thanks for the detailed explanation.

@spring-projects-issues
Copy link
Collaborator Author

Leonard Loesche commented

It looks like the Meta-Annotation for @RefreshScope (as mentioned in this ticket: spring-cloud/spring-cloud-config#190 ) tries to overwrite the default proxyMode (with ScopedProxyMode.TARGET_CLASS) which is unfortunately only working with the annotation at type level (with ClassPathBeanDefinitionScanner and the mentioned ScopeMetadataResolver) but not for @Bean annotated methods, which uses a different approach to resolve the proxyMode.
There is the 'workaround', not to use the meta annotation but the original @Scope with correct proxyMode at the method, but it would be an improvement to have the same behaviour for the custom scope annotation on both sides.

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Leonard Loesche, this issue here is just about the use of a different base scope annotation type, not about meta-annotating with Spring's standard @Scope.

If @RefreshScope or any other @Scope-meta-annotated variant does not work in terms of attribute overriding, this is a bug with meta-annotation processing which you may create a separate JIRA issue for. However, please-double check that it actually fails against Spring Framework 4.2 GA where we significantly overhauled meta-annotation processing.

Juergen

@spring-projects-issues
Copy link
Collaborator Author

spring-projects-issues commented Aug 25, 2015

Leonard Loesche commented

Thank you, for your quick answer!
I originally thought this relates to my case but you are absolutely right: the 'scope' of this ticket is a different one!
So I just opened a new ticket for the mentioned issue: #17974 .
Kind regards,
Leonard

@spring-projects-issues
Copy link
Collaborator Author

Juergen Hoeller commented

Resolving this as "Won't Fix" for the time being, since it's not clear why the use of Spring's @Scope (or derivatives thereof) would need to be avoided on @Bean methods that are Spring-specific anyway.

Juergen

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) status: declined A suggestion or change that we don't feel we should currently apply type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants