Skip to content

Instantly share code, notes, and snippets.

@sarxos
Created June 26, 2015 07:46
Show Gist options
  • Select an option

  • Save sarxos/1c0b697aa04429e1460b to your computer and use it in GitHub Desktop.

Select an option

Save sarxos/1c0b697aa04429e1460b to your computer and use it in GitHub Desktop.
Pure JRE (no HK2, no Spring, no Guice) Java service registration by Maven plugin
<plugins>
<plugin>
<groupId>eu.somatik.serviceloader-maven-plugin</groupId>
<artifactId>serviceloader-maven-plugin</artifactId>
<version>1.0.2</version>
<configuration>
<services>
<service>foo.bar.service.ServiceInterface</service>
</services>
</configuration>
<executions>
<execution>
<goals>
<goal>generate</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
@sarxos
Copy link
Author

sarxos commented Jun 26, 2015

To get instances implementing given service:

import java.util.ServiceLoader;
private Map<String, ServiceInterface> collect() {

    ServiceLoader<ServiceInterface> loader = ServiceLoader.load(ServiceInterface.class);
    Map<String, ServiceInterface> impls = new HashMap<>();

    for (ServiceInterface impl : loader) {
        impls.put(impl.getClass().getSimpleName(), impl);
    }
    return impls;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment