Showing posts with label guice. Show all posts
Showing posts with label guice. Show all posts

Friday, February 07, 2014

ServiceBinder

I've released ServiceBinder 0.2 to Maven Central (details below; soon to be 0.3 with more documentation). I wrote this to fix a common problem for my projects.

I like using the JDK ServiceLoader for discovery: it is simple to use and understand and always available. And Kawaguchi's META-INF/services generator makes use as simple as an annotation.

However, ServiceLoader is missing one key feature for me. It requires a default constructor for implementations, and I am a fan of constructor injection.

ServiceBinder fills this gap. It discovers service classes as ServiceLoader does, and injects them with Guice or Spring. See the GitHub site for examples and source. A taste:

Guice

public final class SampleModule
        extends AbstractModule {
    @Override
    protected void configure() {
        ServiceBinder.with(binder()).bind(Bob.class);
    }
}

Spring Framework

AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
ServiceBinder.with(context).bind(Bob.class);
context.refresh();

Tuesday, December 04, 2012

Guicing your jars, part 2

In Guicing your jars I mention using Java's service loader feature to automate finding modules for Guice, along these lines:

public class MetaInfServicesModule
        extends AbstractModule {
    @Override
    protected void configure() {
        for (final Module module : ServiceLoader.load(Module.class))
            install(module);
    }
}

What I did not mention was the secret sauce, Kohsuke's MetaInfServices annotation processor which creates the META-INF/services file for you:

@MetaInfServices(Module.class)
public class FooModule
        extends AbstractModule {
    @Override
    protected void configure() {
        // Awesome foo-ness here
    }
}

Now using your Maven runtime dependencies to control the classpath, Guice automatically installs your modules:

Guice.createInjector(new MetaInfServicesModule());

(Dependencies between modules is beyond the scope of this post!)

Monday, October 15, 2012

JSR-255 (JMX2) and Guice

On a new project with Guice, then, how do I magically wire my managed objects to JMX? Sadly JSR-255, aka JMX2, did not make JDK6 or JDK7 (there's always JDK8). I live in the here and now, and relied before on Spring JMX annotations and MBeanExporter.

There's no full implementation of JMX2, but covering a good minimal feature set is pojo-mbean. How to get Guice to register @MBean-annotated objects? Use injectors:

class SomeModule extends AbstractModule {
    @Override
    protected void configure() {
        bindListener(Matchers.any(), new JMXTypeListener());
    }
}

class JMXTypeListener implements TypeListener {
    @Override
    public <I> void hear(TypeLiteral<I> type,
            TypeEncounter<I> encounter) {
        Class<? super I> rawType = type.getRawType();
        // From pojo-mbean; eventually JSR-255
        if (rawType.isAnnotationPresent(MBean.class))
            encounter.register(new InjectionListener<I>() {
                @Override
                public void afterInjection(final I injectee) {
                    try {
                        // From pojo-mbean; eventually JSR-255
                        new MBeanRegistration(injectee,
                                objectName(rawType)).
                            register();
                    } catch (Exception e) {
                        encounter.addError(e);
                    }
                }
            });
    }

    private static <I> ObjectName objectName(Class<? super I> type)
            throws MalformedObjectNameException {
        return new ObjectName(type.getPackage().getName(), "type",
                type.getSimpleName());
    }
}

Thursday, August 26, 2010

Guice moving to Maven

It's not new news, but it was new to me: Maven is switching to Guice to wire itself and plugins. From Sonatype:

  1. From Plexus to Guice (#1): Why Guice?
  2. From Plexus to Guice (#2): The Guice/Plexus Bridge and Custom Bean Injection
  3. From Plexus to Guice (#3): Creating a Guice Bean Extension Layer

The plugin framework is named spice inject and glancing through it's straight-forward glue between Guice and OSGi within a Maven object domain.

It looks very clean. Nice.

Wednesday, April 28, 2010

Guicing your jars

Through the magic of ServiceLoader you can automate the wiring of your classpath components:

public static void main(final String... args) {
    final Injector injector = Guice.createInjector(
            ServiceLoader.load(Module.class).iterator());
    // Use injector here, etc.
}

For each jar or classpath component, include a META-INF/services/com.google.inject.Module file containing the class name of a Guice module which can configure the jar, e.g., hm.binkley.rice.MobModule.

This makes your jars auto-configuring. Merely including them in the classpath is sufficient to be injected into your application. You will find multibindings useful for each jar to provide a well-known list of services without them colliding.

Monday, January 26, 2009

Stumbled on: GuiceyFruit

While trying out mvnbrowser I stumbled on GuiceyFruit, a value-add to Guice by James Strachan and Willem Jiang (no blog?).

GuiceyFruit hits the right notes for me: integration of Guice into lots of nice places, especially that EJB3/J2EE business some of my coworkers talk about. Spring-to-Guice converter is cute.

And a bonus: a nice Maven repo tracking Guice 2.x development.

Friday, June 20, 2008

Guice support in IntelliJ IDEA

I just had a surprise reply from Dmitriy Demerov of JetBrains:

The latest Early Access Preview builds of Diana (available at http://www.jetbrains.net/confluence/display/IDEADEV/Diana+EAP ) include a plugin for Guice support. You're very much welcome to give it a try and submit your feedback on it.

I had responded to a sales manager at JetBrains that one item on my wish list for IDEA was direct Guice support.

Yippee!