java - What is the best way to resolve dependencies between my classes when I write a library? -
i'm doing small homemade project , noticed wish reuse portion of code in other projects.
the conclusion move code in separate module , publish in artifactory (i'm using maven) become reusable.
the problem classes within new module rely on each other. usually, when develop non-library project, use spring's inversion of control feature resolve dependencies between classes. can't use dependency injection in module, because dependencies resolved when bean obtained container.
therefore, lets imagine following situation:
i have class1 in module. has field of type class2, should be.. lets say.. instance of same object (i.e. singleton). own ideas how resolve it:
1) if class2 doesn't contain state, methods (and plays utility role) can make static. therefore won't need resolve dependency, since don't need create it's instance anymore. result, can become hard test type of class.
2) can implement singleton pattern without using di. static class2.getinstance() method work, right?
3) can forget idea of using singleton pattern , create instance of class2 "new class2()" expression (inline field declaration or in class1 constructor)
4) somehow use dependency injection in 'magic' way inject bean inside 1 without using container.
what best approach?
Comments
Post a Comment