java - Spring bean initializing dependencies with new keyword? -
i have legacy code class spring bean defined , initialized through xml. singleton field member class dependency. there setter method it, assuming supposed set via spring, although didn't find xml defining it. there get() method dependency, has null check , if null manually creates outside of spring so
class test{ dependency d; setd(dependency d){this.d=d;} getd(){ if(this.d==null){ this.d = new dependency(); }return this.d } }
i trying understand why spring bean initializing dependency outside of spring , implications if any, bad/old design? or not understanding how spring works.
i it's bad design, author wanted provide fall-back case when d not injected in spring. idea attempt make d lazy dependency. should explore what's inside d.
generally can use @required mark members should injected. or use simple , nice constructor injection. if you're concerned lazy injection, that's how spring works default.
Comments
Post a Comment