-1

I have read the following as one of the difference between constructor and setter injection. Can someone explain the below difference with some simple example, I'm bit confused with the term fully function object and incomplete object.

-> Constructor DI allows object to be created in complete state and follows principle of fully functional object while Setter DI allows object to be created without its dependency. which may result in incomplete object if dependency is not available.

  • 1
    Possible duplicate of [Dependency injection through constructors or property setters?](http://stackoverflow.com/questions/1503584/dependency-injection-through-constructors-or-property-setters) – kryger Oct 27 '15 at 14:50
  • Possible duplicate of [Setter DI vs. Constructor DI in Spring?](https://stackoverflow.com/questions/7779509/setter-di-vs-constructor-di-in-spring) – Software Engineer Jul 06 '17 at 23:42

1 Answers1

4

When using Constructor Injection, all required dependencies should be provided through the constructor, as there should not be any setter, to inject them later on. So the instance has all required dependencies!

With Setter Injection, the create instance might be there, but without all the required dependencies (as they have not been set through the setter). A user can use the object without setting de required dependencies.

See also Dependency injection through constructors or property setters?

Community
  • 1
  • 1
Verhagen
  • 3,885
  • 26
  • 36
  • Thanks for the clarification, I got much clear picture of this now. – Shashidhar Boddu Oct 27 '15 at 09:21
  • @Shashidhar Boddu There is this button (arrow up), which says _Answere useful_ and then there is that other V accepting the answer, which shows _which answer was most useful in finding solution / clarification_. Which are easier to use and more rewarding for the person who invests his time in giving an answer. – Verhagen Oct 27 '15 at 13:03