<commit_before>service-binder
==============

Injection parallel to the
[JDK ServiceLoader](http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html).

It comes in two flavors:

* Guice
* Spring Framework

# Motivation

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)`](src/main/java/hm/binkley/util/ServiceBinder.java) for Guice
or `ServiceBinder.with(BeanDefinitionRegistry)` 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.

# Guice example

```java
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) {}
}

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

# Extras

You may find Kohsuke's [META-INF/services generator](https://github.com/binkley/service-binder)
useful to annotate your service implementations: it generates the META-INF services file for you.<commit_msg>More README tests.<commit_after>service-binder
==============

Injection parallel to the
[JDK ServiceLoader](http://docs.oracle.com/javase/7/docs/api/java/util/ServiceLoader.html).

It comes in two flavors:

* Guice
* Spring Framework

# Motivation

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)`](src/main/java/hm/binkley/util/ServiceBinder.java#L73) 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:

```java
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

```java
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

```java
final GenericApplicationContext context = new GenericApplicationContext();
final GenericBeanDefinition catName = new GenericBeanDefinition();
catName.setBeanClass(String.class);
final ConstructorArgumentValues value = new ConstructorArgumentValues();
value.addGenericArgumentValue("Felix");
catName.setConstructorArgumentValues(value);
context.registerBeanDefinition("cat-name", catName);
ServiceBinder.with(context).bind(Bob.class);
```

# Extras

You may find Kohsuke's [META-INF/services generator](https://github.com/binkley/service-binder)
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.
{'fraction_non_alphanumeric': 0.10976998405830107, 'fraction_numerical': 0.0013664313368253246, 'mean_word_length': 4.7486910994764395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 3, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '273633', 'n_tokens_mistral': 1285, 'n_tokens_neox': 1230, 'n_words': 377}