-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
@Named annotation implies @Bean (Prototype) #11667
@Named annotation implies @Bean (Prototype) #11667
Comments
this is not a bug and by design. Adding any qualifier will make it a bean with the default scope (prototype) |
ok, just related to this I wonder - why was prototype chosen as a default scope instead of singleton as it is in Spring? falling back to prototype when any scope is selected has significant impact on the application performance. @wololock discovered this behavior when he was debugging huge memory leak (thousands of repository instances in the memory). |
Just to give you more context, there's one nasty corner case when injecting a class with only This was our use case - one of our microservices that was using a few I guess if non-duplicated Microaut Data repositories were injected in such a case (like it happens with other beans), the default behavior wouldn't produce any weird side-effects. |
@wololock Can you please create a spearate issue with a test? |
@dstepanov I think I have enough info to add it to my test repo. let me check. |
Nevermind, found the problem |
Thank you, @dstepanov ! |
you're fast. here's the test anyway musketyr/named-annotation-bean-issue#1 |
so this look like a separate bug. As for Micronaut default vs Spring default one could inverse the question. In CDI and other dependency injectors singleton is not the default. |
thanks for explanation @graemerocher. I will keep this in mind. I got the question from my colleague as almost everyone comes from Spring background. |
Expected Behavior
When a class is annotated only with
@Named
annotation and asked for injection then the bean locator throwsNoSuchBeanException
because of missing scope annotation such as@Singleton
Actual Behaviour
@Named
annotation works as a synonym for@Bean
and implies prototype scope. Every time the bean locator is asked for the bean, the new one is created.Steps To Reproduce
@Named
annotation such as@Named("foo") public class NamedBean { }
beanLocator.getBean(NamedBean.class)
Environment Information
No response
Example Application
https://github.com/musketyr/named-annotation-bean-issue for reproducer
Version
4.7.6
The text was updated successfully, but these errors were encountered: