问题
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:
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?
来源:https://stackoverflow.com/questions/33361027/constructor-injection-vs-setter-injection