1

I recently started working with Java after 7 years coding in .Net, don't get me wrong i LOVE C# but the job demanded and i needed a change.

My question is about Ioc registration, in C# I've used Windsor and SimpleInjector (which i totally love) and in java i want to start using Guice.

The project that i am working on is a legacy project that does not use Ioc per say, it's a Jersey Project (with non functional HK2 IOC).

And now for my question, on C# I've registered all interfaces and concrete implementation on application start in runtime using reflection.

I've read Guice documentation and could not find any example to do so, and the JIT binding is not really understood.

So is it possible to do so? is it best practice? (i don't think that manually writing all binding is the right course)

Any examples or direction would be very helpful

Thank you

guyl
  • 2,158
  • 4
  • 32
  • 58
  • How do you mean a non-functional Hk2. Hk2 is quite functional... Hk2 also has a robust framework for automatic service registration using @Service and ServiceLocator populators – jwells131313 Feb 28 '17 at 01:56
  • @jwells131313, what i've meant is that HK2 in our system is not operational. – guyl Feb 28 '17 at 11:34

1 Answers1

3

This is a bit harder to do in Java and Guice than on other platforms: The Java classloader is designed for finding classes by their package/name with fallbacks more so than enumerating every class or implementation on the classpath. Though some library solutions exist to do the latter under prescribed conditions (e.g. Reflections or Google Guava), it is generally impossible to iterate across every class available through the current ClassLoader and its parents as the set is theoretically infinite through URLClassLoader and similar network-based classloaders.

This makes it common and often necessary to explicitly list your interfaces and implementations to expose to your application, whether or not you're using Guice.

See:

Consequently, it is difficult to accomplish tasks like "start every reachable class that implements MyStartableService", though through the Guice SPI you may have more luck with "start every MyStartableService requested through Guice before returning it". Alternatively, you could also use Java's SPI framework built in Java 6 (e.g. ServiceLoader) to allow for sharded or distributed lists of appropriate services, which can then be supplied to Guice through calls to bind in a Module. (Olivier aptly suggests AutoService in the comments, which makes it easy to generate the metadata files that power ServiceLoader.)

Community
  • 1
  • 1
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
  • 1
    If you go the SPI framework-way, try using Google's [AutoService](https://github.com/google/auto/tree/master/service): it's a life-saver: you don't touch any non-java file, you just add a small annotation and everything is done for you automatically. – Olivier Grégoire Feb 22 '17 at 01:54
  • thank you for your detailed answer, i'll try to understand how to act next – guyl Feb 22 '17 at 12:19