public interface MyService { void doSomething(); } @AutoService(MyService.class) public class MyServiceImpl implements MyService { @Override public void doSomething() { System.out.println("Doing something..."); } } Maven: <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <annotationProcessorPaths> <path> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>1.0-rc6</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> Gradle: groovy dependencies { annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6' compileOnly 'com.google.auto.service:auto-service:1.0-rc6' } import java.util.ServiceLoader; public class Main { public static void main(String[] args) { ServiceLoader<MyService> serviceLoader = ServiceLoader.load(MyService.class); for (MyService service : serviceLoader) { service.doSomething(); } } }


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