===== Domino-auto is a simple and lightweight service loader for GWT/J2CL application that uses annotation processor to create a loader class that load instances of a specific service class.
- The API dependency
<dependency>
<groupId>org.dominokit</groupId>
<artifactId>domino-auto-api</artifactId>
<version>[version]</version>
</dependency>
- The Processor dependency
<dependency>
<groupId>org.dominokit</groupId>
<artifactId>domino-auto-processor</artifactId>
<version>[version]</version>
<scope>provided</scope>
</dependency>
Or as s processor path in the compiler plugin :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<!-- path to your annotation processor -->
<path>
<groupId>org.dominokit</groupId>
<artifactId>domino-auto-processor</artifactId>
<version>[version]</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
Adding the dependencies should be all you need to start using the tool; it will automatically generate service loaders for all services defined in the classpath.
The generated service loader class name will follow the convention [Service name]_ServiceLoader
, and will provide a
single method load
that returns a list of that service implementations.
The user needs to specify an include list of packages that will be included in the generation, the provided list
represent the package of the implemented service class not the implementations. the include list can be configured using a
compiler argument dominoAutoInclude
or using @DominoAuto
annotation on a type or package-info.
- Make sure the service package is added to the include parameter of the annotation :
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<annotationProcessorPaths>
<!-- path to your annotation processor -->
<path>
<groupId>org.dominokit</groupId>
<artifactId>domino-auto-processor</artifactId>
<version>[version]</version>
</path>
</annotationProcessorPaths>
<compilerArgs>
<arg>-AdominoAutoInclude=com.dominokit.samples</arg>
</compilerArgs>
</configuration>
</plugin>
Using this method, make sure in case you are building from the ide to setup the compiler arguments in the ide or make the ide delegate the build to maven
or
@DominoAuto(include = {"com.dominokit.samples"})
package com.dominokit.samples;
import org.dominokit.auto.DominoAuto;
Lets define the desired service interface
package com.dominokit.samples;
public interface SampleService {
void init();
}
The lets have different implementations
package com.dominokit.samples;
public class FooSampleServiceImpl implements SampleService {
public void init(){
//Do something here
}
}
//-------------
package com.dominokit.samples;
public class BarSampleServiceImpl implements SampleService {
public void init(){
//Do something here
}
}
The project META-INF
we register both implementations as services :
Create a file named com.dominokit.samples.SampleService
under the following path
project root folder -> src -> main -> resources -> META-INF -> services
In the file list the services with the full qualified class name
com.dominokit.samples.FooSampleServiceImpl
com.dominokit.samples.BarSampleServiceImpl
This will generate the following code :
public class SampleServiceAuto_ServiceLoader {
public static List<SampleService> load() {
List<SampleService> services = new ArrayList();
services.add(new FooSampleServiceImpl());
services.add(new BarSampleServiceImpl());
return services;
}
}
Then we can use it like this :
SampleService_ServiceLoader.load()
.forEach(SampleService::init);