23 December 2020

SCR -Dependency injection

Constructor Injection

  • It does not allow you to construct object until the dependency are ready. Partial injection is not possible.
  • Every time the constructor is called and new object is created, hence the dependency can’t be overridden. Constructor injection is immutable.
  • It uses the Index to inject the dependency.
  • If there are more number dependencies constructor injection will be the better one since for setter we need to have more setter properties.
  • primitive and String-based values, Dependent object (contained object),Collection values etc. can be injected using constructor injection.

Setter Injection

  • Setter injections are more readable since the field name will be same in the setter method.
  • Setter injection doesn’t ensure that all the dependencies are injected. Dependency injection may be incomplete. Partial injection is allowed.
  • Setter injection overrides the constructor injection in case both are used in the application.
  • Values can be easily modified using setter injection. It doesn’t not create a new bean instance always as constructor injection. Setter injection is mutable.
  • Primitive and String-based values, Dependent object (contained object),Collection values etc. can be injected using setter injection.

No comments:

Post a Comment

Most views on this month