In what cases would you choose constructor injection over setter injection, and why?

  • When you need to inject dependencies that are immutable and required for the object to function properly.
  • When you want to inject dependencies after the object is created.
  • Constructor injection should always be preferred over setter injection.
  • When you need to inject dependencies that may change during the object's lifecycle.
Constructor injection is preferred over setter injection when you need to inject dependencies that are immutable and required for the object to function properly. This ensures that the object is in a valid state from the moment it is created. Setter injection is more suitable when you want to inject optional or mutable dependencies after the object is constructed. The other options are not accurate reasons for choosing one injection method over the other.
Add your answer
Loading...

Leave a comment

Your email address will not be published. Required fields are marked *