Java spi profile generated automatically using google autoservice

spi is a standard service discovery, for the development, we usually need to write META-INF/servicesa file folder defined in the class.
google auto in autoservice can help us generate the corresponding configuration, it is convenient

Preparing the Environment

  • Project structure
 
├── pom. xml
└── src
    ├── main
    ├── java
    └── with
    └── dalong
    ├── sleeps
    └── TranslationService.java
    └── spimpl
    ├── BingTranslationServiceProvider.java
    └── GoogleTranslationServiceProvider.java
    └── resources
    └── test
        └── java
            └── with
                └── dalong
                    └── SPITestClass.java
 
      
  • Code Description
    conventions:
    SPI interfaces defined in TranslationService.java
    BingTranslationServiceProvider.java to implement the interface and GoogleTranslationServiceProvider.java
    pom.xml file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <encoding>UTF-8</encoding>
        <java.version>1.8</java.version>
        <maven.compiler.source>1.8</maven.compiler.source>
        <maven.compiler.target>1.8</maven.compiler.target>
        <auto-service.version>1.0-rc6</auto-service.version>
    </properties>
    <groupId>com.dalong.appdemo</groupId>
    <artifactId>first</artifactId>
    <version>1.0-SNAPSHOT</version>
<dependencies>
    <dependency>
        <groupId>com.google.auto.service</groupId>
        <artifactId>auto-service-annotations</artifactId>
        <version>${auto-service.version}</version>
        <optional>true</optional>
        <scope>compile</scope>
    </dependency>
        <dependency>
            <groupId>com.google.auto.service</groupId>
            <artifactId>auto-service</artifactId>
            <version>${auto-service.version}</version>
            <optional>true</optional>
            <scope>compile</scope>
        </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.8</version>
        <scope>test</scope>
    </dependency>
</dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-site-plugin</artifactId>
                <version>3.7</version>
                <dependencies>
                    <dependency>
                        <groupId>org.apache.maven.doxia</groupId>
                        <artifactId>doxia-site-renderer</artifactId>
                        <version>1.8</version>
                    </dependency>
                </dependencies>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <annotationProcessorPaths>
                        <path>
                            <groupId>com.google.auto.service</groupId>
                            <artifactId>auto-service</artifactId>
                            <version>${auto-service.version}</version>
                        </path>
                    </annotationProcessorPaths>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>
 

Interface Definition:
TranslationService.java

 
package com.dalong.spi;
import java. useful. local;
public interface TranslationService {
    String translate(String message, Locale from, Locale to);
}
 

Realization:
BingTranslationServiceProvider.java and GoogleTranslationServiceProvider.java

 
package com.dalong.spimpl;
import com.dalong.spi.TranslationService;
import com.google.auto.service.AutoService;
import java. useful. local;
@AutoService(TranslationService.class)
public class BingTranslationServiceProvider implements TranslationService {
    @Override
    public String translate(String message, Locale from, Locale to) {
        return "bing";
    }
}
package com.dalong.spimpl;
import com.dalong.spi.TranslationService;
import com.google.auto.service.AutoService;
import java. useful. local;
@AutoService(TranslationService.class)
public class GoogleTranslationServiceProvider implements TranslationService {
    @Override
    public String translate(String message, Locale from, Locale to) {
        return "google";
    }
}
 
 

Construction of packing

mvn  clean  package

effect

 

 

 

test

  • Code
package com.dalong;
import com.dalong.spi.TranslationService;
import com.sun.prism.shader.AlphaOne_Color_AlphaTest_Loader;
import org.junit.Test;
import java. useful. local;
import java.util.ServiceLoader;
import java.util.function.Consumer;
import java.util.stream.StreamSupport;
import static org.junit.Assert.assertEquals;
public class SPITestClass {
    @Test
    public void myTestForSPI(){
        ServiceLoader<TranslationService> loader = ServiceLoader.load(TranslationService.class);
        long count = StreamSupport.stream(loader.spliterator(), false).count();
        loader.forEach(new Consumer<TranslationService>() {
            @Override
            public void accept(TranslationService translationService) {
                Locale localeFrom = new Locale("en");
                Locale localeFo = new Locale("zh");
                String message = translationService.translate("app", localeFrom,localeFo);
                System.out.println(message);
            }
        });
        assertEquals(2, count);
    }
}
 

Explanation

Based on spi we can develop more flexible modular system, and can be easily inserted and removed processing module

Reference material

https://github.com/google/auto/tree/master/service
https://www.jianshu.com/p/46b42f7f593c

Guess you like

Origin www.cnblogs.com/rongfengliang/p/11695684.html