Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Guice + classpath scanning

Tags:

java

guice

I am looking at Guice at the moment and it would appear that its geared towards explicit programmatic building of the context via modules.

Now I am fairly used to using annotations to put something into the context and using classpath scanning to build the context.

Now I could fairly easily add this "feature" to guice, but I'd rather not reinvent the wheel, so if someone knows if there is an extension that already does this - please say.

However, my question is, would I be breaking the desired use and design of Guice by doing this...have I missed the point of how/why guice is meant to be used in an enterprise application?

like image 275
Cheetah Avatar asked Jul 14 '15 10:07

Cheetah


1 Answers

Guice has some pretty smart JIT binding that makes it unnecessary to scan-per type (concrete types for example) in most situations if there is not an actual binding required (e.g. interface-to-implementation, etc.).

I personally find package-scanning for components in spring to be a mess. The fact that you have to explicitly filter out what you don't want, and get 'sub-packages' (which mean nothing in the language) scanned by default, with no simple way to do just the package your types are in (without ugly filter code and reflection) is extremely fragile and error-prone. Guice's approach to this is far more elegant (convention of Module per package). So in short, I find myself being explicit in spring most of the time to maintain my sanity :)

like image 53
leeor Avatar answered Nov 02 '22 12:11

leeor