Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
158 views
Welcome To Ask or Share your Answers For Others

1 Answer

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...