Service Binder

Injection parallel to the SDK ServiceLoader

Лицензия

Лицензия

Группа

Группа

hm.binkley
Идентификатор

Идентификатор

service-binder
Последняя версия

Последняя версия

0.3
Дата

Дата

Тип

Тип

jar
Описание

Описание

Service Binder
Injection parallel to the SDK ServiceLoader
Ссылка на сайт

Ссылка на сайт

http://binkley.blogspot.com
Система контроля версий

Система контроля версий

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

Скачать service-binder

Как подключить последнюю версию

<!-- 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"]

Зависимости

compile (5)

Идентификатор библиотеки Тип Версия
com.google.code.findbugs : jsr305 Необязательный jar 2.0.3
org.kohsuke.metainf-services : metainf-services Необязательный jar 1.5
com.google.inject : guice Необязательный jar 3.0
com.google.inject.extensions : guice-multibindings Необязательный jar 3.0
org.springframework : spring-context Необязательный jar 4.0.1.RELEASE

test (2)

Идентификатор библиотеки Тип Версия
org.hamcrest : hamcrest-library jar 1.3
junit : junit jar 4.11

Модули Проекта

Данный проект не имеет модулей.

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.

Версии библиотеки

Версия
0.3
0.2