Is it good to use dependency injection in a java library?

udayanga :

I have a play application and want to take a common operation out from the application and make it as a library in order to use in other play applications.

This proposing library has a contract(interface) and several implementations of top of that.

What if I use guice to bind contact and implementations with named injection(using annotations) and call them with the appropriate annotation in the play application without instantiating library classes in the apply application?

Deepak gupta :

It is fine to use DI in a library, although that would add a restriction that the library users will be forced to use DI as well. I usually use DI in the java libraries that I write but then I wrap the client-facing interfaces with factories. That way, you get best of both worlds!

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=156072&siteId=1