What is the best approach for dependency injection if Spring or Guice are not used?
If your library was written in an idiom-friendly idiom. It should be easy enough to use as a simple Java API. Think about your past experiences with spring. There are several libraries that are great for the Spring model, but were written before Spring time. I don't see anything wrong with new , followed by a pair of setXX , followed by a call to the real working method. Just be careful, because, among other things, your client may forget to call the thouse init methods, which are reliable Spring.
Should I consider something like a factory method for instantiating and wiring objects? Dependencies are inside the library, so it does not seem advisable for the application to instantiate each dependency to create the main object.
Let the client application solve this. You provide a library. Let the API client attach its own objects. Give an example. Later, the same example can be used to create a factory method in a client domain. Perhaps the client application has its own way of configuring itself, and it would be desirable if the API that your library provided was flexible enough to take advantage of this.
Or maybe you can enable guice. License is Apache. Like the whole piece of Java itself.
Marcelo morales
source share