Assuming they all implement a common Interface, take a look at MapBinder and/or building a Map instance to inject. "TX1" would be the key, the class corresponding to that key would be the value. In your Servlet you would then @Inject the Map.
If you don't know all your implementations, and again assuming they all implement a common Interface or extend a common base Class, you could use something like Guava's ClassPath Class to find all your implementations at startup and add them to the Map. On Wed, May 13, 2015 at 10:02 AM, David <[email protected]> wrote: > As I recently learned in this group: Guice does not allow n:1 bindings. > > So you have an option to either: > - define named bindings for the thousands of service classes > - define a factory class with a method that takes a string and returns the > service instance. Then you just need to bind an implementation of the > factory that uses your reflection code. > > I guess the last option can be enhanced to support injecting dependencies > in the service by injecting the Injector in the Factory implementation and > using it to get an instance of the service. > > > > On Wed, May 13, 2015 at 11:26 AM, klc <[email protected]> wrote: > >> Hi guys, >> >> I have a servlet application that use reflection to find service class, >> for example a URL: >> >> abc.com/contextroot/asdf?TX=TX1: A servlet is dedicated to 'asdf', it >> use 'TX1' to lookup the configuration and get service class name, and use >> Class.forName().newInstance() to get the instance. >> >> Now I want to integrated Guice into this system. And there and thousands >> of service class.....what's the right way to do it? >> I can use annotation binding or @Named, but that will be thousands of >> class to change... >> Can I put the reflection code (.newInstance()) into a provider? but the >> Provider interface 'get()' has no argument >> >> klc >> >> -- >> You received this message because you are subscribed to the Google Groups >> "google-guice" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected]. >> To post to this group, send email to [email protected]. >> Visit this group at http://groups.google.com/group/google-guice. >> To view this discussion on the web visit >> https://groups.google.com/d/msgid/google-guice/fd20026d-f89e-494a-9180-c07eed9f2f95%40googlegroups.com >> <https://groups.google.com/d/msgid/google-guice/fd20026d-f89e-494a-9180-c07eed9f2f95%40googlegroups.com?utm_medium=email&utm_source=footer> >> . >> For more options, visit https://groups.google.com/d/optout. >> > > -- > You received this message because you are subscribed to the Google Groups > "google-guice" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/google-guice. > To view this discussion on the web visit > https://groups.google.com/d/msgid/google-guice/CABrJHW2bt%2Bq%3DaENw2V1eh%2Bnk61y-AKnjLi3Ceyuhj9X%3DNDO6Tw%40mail.gmail.com > <https://groups.google.com/d/msgid/google-guice/CABrJHW2bt%2Bq%3DaENw2V1eh%2Bnk61y-AKnjLi3Ceyuhj9X%3DNDO6Tw%40mail.gmail.com?utm_medium=email&utm_source=footer> > . > > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "google-guice" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/google-guice. To view this discussion on the web visit https://groups.google.com/d/msgid/google-guice/CAMBZqfCAfJ5Jc6c6nTtxyuQ%2B7_xVpma3UHeokO5E8YETUUHEpQ%40mail.gmail.com. For more options, visit https://groups.google.com/d/optout.
