Which one is better setter injection or constructor injection?



Which one is better setter injection or constructor injection?..

Answer / Nitin Kumar

Both setter injection and constructor injection are effective methods of dependency injection, but they have their own trade-offs. Setter injection allows for more flexibility in terms of initializing objects, but it can lead to problems like circular dependencies and makes the code slightly harder to test. Constructor injection is considered cleaner, as it enforces that all dependencies are set upfront during object creation, which results in less cluttered code and easier testing.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Spring Framework Interview Questions

What are some of the important features and advantages of spring framework?

1 Answers  


What do you mean by @qualifier in spring?

1 Answers  


In spring, what is weaving?

1 Answers  


How can you achieve internationalization using messagesource?

1 Answers  


Can I learn spring boot without spring? : Spring Boot

1 Answers  


Different spring bean scope.

1 Answers  


What is spring @autowired?

1 Answers  


What are the differences between bean factory and application context?

1 Answers  


What are the important beans lifecycle methods?

1 Answers  


What are some of the important features and benefits of the spring framework?

1 Answers  


Why spring is loosely coupled?

1 Answers  


Why do we use spring mvc?

1 Answers  


Categories