=================== --------- -------------- import com.google.auto.service.AutoService; @AutoService(MyService.class) public class MyServiceImpl implements MyService { } com.example.MyService ------------ <dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>1.0-rc7</version> </dependency> public interface MyService { void doSomething(); } import com.google.auto.service.AutoService; @AutoService(MyService.class) public class MyServiceImpl1 implements MyService { @Override public void doSomething() { System.out.println("MyServiceImpl1 is doing something."); } } import com.google.auto.service.AutoService; @AutoService(MyService.class) public class MyServiceImpl2 implements MyService { @Override public void doSomething() { System.out.println("MyServiceImpl2 is doing something."); } } import java.util.ServiceLoader; public class Client { public static void main(String[] args) { ServiceLoader<MyService> serviceLoader = ServiceLoader.load(MyService.class); for (MyService service : serviceLoader) { service.doSomething(); } } } MyServiceImpl1 is doing something. MyServiceImpl2 is doing something. ------------ <build> <plugins> <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>1.0-rc7</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> ----------


上一篇:
下一篇:
切换中文