java - Setter Injection vs. Constructor Injection -
java - Setter Injection vs. Constructor Injection -
in pro spring 3 book, chapter 4 - introduction ioc , di in spring - page 59, in "setter injection vs. constructor injection" section, paragraph says
spring included, provide mechanism ensuring dependencies defined when utilize setter injection, using constructor injection, assert requirement dependency in container-agnostic manner"
could explain examples
a class takes required dependency constructor argument can instantiated if argument provided (you should have guard clause create sure argument not null.) constructor hence enforces dependency requirement whether or not you're using spring, making container-agnostic.
if utilize setter injection, setter may or may not called, instance may never provided dependency. way forcefulness setter called using @required
, specific spring , hence not container-agnostic.
so maintain code independent of spring, utilize constructor arguments injection.
java spring dependency-injection required
Comments
Post a Comment