0 votes
in JAVA by
Which one is better constructor injection or setter dependency injection?

1 Answer

0 votes
by

Each has their own advantage and disadvantage. Constructor injection guaranteed that class will be initialized with all its dependency, but setter injection provides flexibility to set an optional dependency. Setter injection is also more readable if you are using an XML file to describe dependency. Rule of thumb is to use constructor injection for mandatory dependency and use setter injection for optional dependency.

Related questions

+1 vote
asked May 30, 2020 in JAVA by Robindeniel
+2 votes
asked Jun 2, 2020 in JAVA by Indian
...