discovering services/modules via SPI

77 views
Skip to first unread message

steve christensen

unread,
Jan 23, 2015, 2:48:32 AM1/23/15
to gwi...@googlegroups.com
I stumbled on Google's auto-service project the other day, which made me dislike the SPI pattern less. You'd annotated your Guice modules with auto-service's @AutoService(Module.class) annotation. That generates a META-INF/services/com.google.inject.Module at compile time for the annotated class.

So... rather than compose an application via Maven dependencies, and manually adding each dependency's modules into the injector like: 

final Injector injector = Guice.createInjector(
new ExampleModule(),
new ConfigModule(new File(args[0]), ExampleConfig.class),
new LoggingModule(),
new RestModule(),
new HibernateModule(),
new MetricsModule());

injector.getInstance(Run.class).start();


You'd compose the application via Maven, then to get all the default Modules loaded into your injector, you'd do something like:

final Injector injector = Guice.createInjector(
new ExampleModule(),
new ConfigModule(new File(args[0]), ExampleConfig.class),
GwizardModuleLoader.loadModules() 
);

// start services
injector.getInstance(Run.class).start(); // if you depend  on gwizard-services



I took a stab at integrating it here: https://github.com/stevesea/gwizard/tree/spi/gwizard-spi 
It still has lots of rough edges. And... I'm not sure I like it. But, thought I'd throw it out to see if there's interest

Jeff Schnitzer

unread,
Jan 23, 2015, 4:52:07 AM1/23/15
to gwi...@googlegroups.com
Hmmmmmm... not sure how I feel about this either. It does seem kind of nice, but it adds complication when building a fat jar (need to enable the ServicesResourceTransformer in the shade plugin). And I'm just generally allergic to the META-INF dir.

It seems a no-brainer to include the autoservice flag on the modules, though. Then just not document it until we're sure we like it :)

Maybe the ModuleLoader will be what pushes us to finally create a gwizard-common jar.

Jeff

--
You received this message because you are subscribed to the Google Groups "GWizard Discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gwizard+u...@googlegroups.com.
To post to this group, send email to gwi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gwizard/c2a6c80d-9f42-43cb-934b-c847bcbcafe1%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

steve christensen

unread,
Jan 23, 2015, 4:20:57 PM1/23/15
to gwi...@googlegroups.com
I like that google auto-service eliminated my major problem with the SPI pattern -- having to add that file to META-INF, and not fat-finger the spelling of the filename, and not fat-finger the spelling of the contents within the file, and keep both of those synchronized with whatever refactoring was going on in the codebase. 


thinking about it more today... I think I favor the explicitness of: 

     final Injector injector = Guice.createInjector(
new ExampleModule(),
new ConfigModule(new File(args[0]), ExampleConfig.class),
new LoggingModule(),
new RestModule(),
new HibernateModule(),
new MetricsModule());

over a SPI module loader approach.

I also like that the individual gwizard components can depend on each other with just an install() statement in their Modules. So, if someone was tired of stringing all those "new *Module()"s together, they could create a 'StandardGwizardWebAppModule' that just install()s all the standard components that make up a web app, and people could use it or not.



--
You received this message because you are subscribed to a topic in the Google Groups "GWizard Discussion" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/gwizard/hbVyyfcDJxk/unsubscribe.
To unsubscribe from this group and all its topics, send an email to gwizard+u...@googlegroups.com.

To post to this group, send email to gwi...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages