-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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 DI layer ingores @Scope#scopeName #12233
Comments
Spring' @scope#scopeName is now taken into account
I have a sample application declaring beans as prototypes, but it is not working as expected in Spring framework. A copy of the application is available in the branch feature/quarkus-edition of my repo. My configuration class where I declared the prototype bean is:
That been it used in a Service class, declared as:
This class has a method to use the prototype instance each time is invoked. The first time invoked works however when I invoke a second time then the
So the service class is using a singleton instance of the I know that a good practice is use singleton instances, however this approach is valid in Spring and it is common to see in many projects. Why it is not working in my case? |
@rmarting after looking into this, my conclusion is that the Quarkus Spring DI behavior is correct. |
Sorry @geoand but it seems that your comments are wrong. I annotated the service class with The original code running in Spring does not force me to annotate the service as prototype, basically because I use an instance of
That implementation works successfully in Spring framework. So Quarkus Extension for Spring DI is not covering successfully this scope ... this sample code demostrate it. (Maybe I am wrong in something else in the Quarkus implementation of my project) but the original code from Spring works. Ideas? |
I tried this as well, and when I did, a new
I am assuming that
See my first comment. |
But did you tried to invoke twice the REST endpoint? Because the first time works successfully, but when you invoke again the same endpoint, then the exception appears. The problem here is the Producer or Consumer instance is the same, and when you tried to invoke to send or poll messages from Apache Kafka then some internal objects are closed, because the service is reusing the previous instance closed after the first method invokation. |
Then it sounds you want the Controller to be request scoped, no? |
Annotated all the componentes (RestControllers, Service and Beans) as prototypes then the application works. (Code pushed into the branch). However I am not sure it is the best approach to migrate this case from Spring to Quarkus Spring DI Extension. I am trying to identify the best approach to migrate the original Spring code in this application using the Quarkus Extensions for Spring. Basically I would like to identify if I can use the original source code without refactor to CDI. If I have to add more extra definitions in my classes, then the best approach should be to refactor to CDI instead of to mantain the original Spring code. I found this issue with prototypes and I only want to understand the best steps to migrate without a refactor. I refactored in other branch to use CDI annotations and there it is working successfully. |
I understand your point and it makes perfect sense. To be honest, it seems to me that the pure Spring application worked by accident, or by some obsure Spring feature that switched the scope of beans. |
The Spring framwork best practices say that: use Singletons ... however my field experience showed me that from time to time a So, finally if Quarkus Spring DI could manage successfully the Where we could describe this approach? Blog? Quarkus Guide? |
I would say that there is no hard rule, but that does make sense - if you are doing obscure DI stuff (which you are not even sure would work in Spring proper), then it probably makes sense to migrate to CDI
I'll let @jclingan comment on this :) |
@scope#value however does work
The text was updated successfully, but these errors were encountered: