Announcing Sangria

Tavian Barnes GitHub

Sangria is a new project of mine to release various Guice extensions I've been working on recently. Right now the coolest thing it can do is context-sensitive injections, allowing (among other things) first-class Logger injection for more than just java.util.logging.

For example, so allow SLF4J Logger injection, all you have to do is this:

import com.google.inject.AbstractModule;
import com.tavianator.sangria.slf4j.SangriaSlf4jModule;

public class YourModule extends AbstractModule {
    @Override
    protected void configure() {
        install(new SangriaSlf4jModule());
    }
}

And now this will just work:

import org.slf4j.Logger;

public class YourClass {
    private final Logger logger;

    @Inject
    YourClass(Logger logger) {
        this.logger = logger;
    }
}

To create your own context-sensitive injections, implement the ContextSensitiveProvider interface:

import com.tavianator.sangria.contextual.ContextSensitiveProvider;

public class YourProvider implements ContextSensitiveProvider<YourType> {
    @Override
    public YourType getInContext(InjectionPoint injectionPoint) {
        // Create an instance. The type you're being injected into is available
        // as injectionPoint.getDeclaringType().
    }

    @Override
    public YourType getInUnknownContext() {
        // Create an instance for an unknown context. The context will be
        // unknown for Provider<YourType> bindings and for Provider method
        // parameters. If the context is required, it is valid to throw an
        // exception here.
    }
}

Then use ContextSensitiveBinder to bind it:

import com.google.inject.AbstractModule;
import com.tavianator.sangria.contextual.ContextSensitiveBinder;

public class YourModule extends AbstractModule {
    @Override
    protected void configure() {
        ContextSensitiveBinder.create(binder())
                .bind(YourType.class)
                .toContextSensitiveProvider(YourProvider.class);
    }
}

Sangria is released under the Apache License, Version 2.0.