#is it bad practice to pass around an autowired instance in spring boot?
1 messages ยท Page 1 of 1 (latest)
Here is an AI assisted attempt to answer your question ๐ค. Maybe it helps! In any case, a human is on the way ๐. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.
It is generally considered bad practice to pass around autowired instances in Spring Boot because it can lead to tight coupling between classes, making the code harder to maintain and test. It is better to use dependency injection to provide the necessary dependencies to each class individually.
In this specific scenario, if Class B needs both X (autowired in Class A) and a String value, it would be better to autowire X directly in Class B or pass it as a method parameter if needed. This way, Class B can have a clear dependency on X without relying on Class A to provide it.
By following the principles of dependency injection and separating concerns between classes, you can improve code readability, flexibility, and testability. Consider refactoring your code to follow this approach for better adherence to best practices in Spring Boot development.
@crisp turtle
Your question has been closed due to inactivity.
If it was not resolved yet, feel free to just post a message below
to reopen it, or create a new thread.
Note that usually the reason for nobody calling back is that your
question may have been not well asked and hence no one felt confident
enough answering.
When you reopen the thread, try to use your time to improve the quality
of the question by elaborating, providing details, context, all relevant code
snippets, any errors you are getting, concrete examples and perhaps also some
screenshots. Share your attempt, explain the expected results and compare
them to the current results.
Also try to make the information easily accessible by sharing code
or assignment descriptions directly on Discord, not behind a link or
PDF-file; provide some guidance for long code snippets and ensure
the code is well formatted and has syntax highlighting. Kindly read through
https://stackoverflow.com/help/how-to-ask for more.
With enough info, someone knows the answer for sure ๐