Service Binder

Injection parallel to the SDK ServiceLoader

License

License

GroupId

GroupId

hm.binkley
ArtifactId

ArtifactId

service-binder
Last Version

Last Version

0.3
Release Date

Release Date

Type

Type

jar
Description

Description

Service Binder
Injection parallel to the SDK ServiceLoader
Project URL

Project URL

http://binkley.blogspot.com
Source Code Management

Source Code Management

https://github.com/binkley/service-binder

Download service-binder

How to add to project

<!-- https://jarcasting.com/artifacts/hm.binkley/service-binder/ -->
<dependency>
    <groupId>hm.binkley</groupId>
    <artifactId>service-binder</artifactId>
    <version>0.3</version>
</dependency>
// https://jarcasting.com/artifacts/hm.binkley/service-binder/
implementation 'hm.binkley:service-binder:0.3'
// https://jarcasting.com/artifacts/hm.binkley/service-binder/
implementation ("hm.binkley:service-binder:0.3")
'hm.binkley:service-binder:jar:0.3'
<dependency org="hm.binkley" name="service-binder" rev="0.3">
  <artifact name="service-binder" type="jar" />
</dependency>
@Grapes(
@Grab(group='hm.binkley', module='service-binder', version='0.3')
)
libraryDependencies += "hm.binkley" % "service-binder" % "0.3"
[hm.binkley/service-binder "0.3"]

Dependencies

compile (5)

Group / Artifact Type Version
com.google.code.findbugs : jsr305 Optional jar 2.0.3
org.kohsuke.metainf-services : metainf-services Optional jar 1.5
com.google.inject : guice Optional jar 3.0
com.google.inject.extensions : guice-multibindings Optional jar 3.0
org.springframework : spring-context Optional jar 4.0.1.RELEASE

test (2)

Group / Artifact Type Version
org.hamcrest : hamcrest-library jar 1.3
junit : junit jar 4.11

Project Modules

There are no modules declared in this project.

service-binder

Injection parallel to the JDK ServiceLoader.

It comes in two flavors:

  • Guice
  • Spring Framework

Motivation

This section needs revision in light of non-support in Guice 3.0 of injecting modules.

Using ServiceLoader to find modules to install into Guice is straight-forward:

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

However this does not provide modules with injection. Spring Framework handles this better.

Using ServiceBinder provides the same discovery mechanism and provides injection. It is not limited to modules.

Pick an injector

Use ServiceBinder.with(Binder) for Guice or [ServiceBinder.with(BeanDefinitionRegistry)] (src/main/java/hm/binkley/util/ServiceBinder.java#L77) for Spring Framework. These return an With implementation specific to your choice.

Bind services

Use With.bind(Class) or With.bind(Class, ClassLoader). If not provided bind() uses the thread-context class loader.

Examples

Examples assume these services:

public interface Bob {}

@MetaInfServices
public static final class Fred
        implements Bob {}

@MetaInfServices
public static final class Nancy
        implements Bob {
    @Inject
    public Nancy(@Named("cat-name") final String catName) {}
}

Guice example

public final class SampleModule
        extends AbstractModule {
    @Override
    protected void configure() {
        bindConstant().annotatedWith(named("cat-name")).to("Felix");
        ServiceBinder.with(binder()).bind(Bob.class);
    }
}

Spring example

@Configuration
public static class AppConfig {
    @Bean(name = "cat-name")
    public String catName() {
        return "Felix";
    }
}
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
context.register(AppConfig.class);
ServiceBinder.with(context).bind(Bob.class);
context.refresh();

Extras

You may find Kohsuke's META-INF/services generator useful to annotate your service implementations: it generates the META-INF services file for you.

When using the maven shade plugin you may also find [the services transformer] (https://maven.apache.org/plugins/maven-shade-plugin/examples/resource-transformers.html#ServicesResourceTransformer) useful to merge META-INF/services files.

Releases

0.3

  • Use ServiceConfigurationError rather than ServiceBinderError
  • Documentation
  • Improved Spring unit tests

0.2

  • Support for Guice
  • Support for Spring Framework

0.1

Not released.

Versions

Version
0.3
0.2