<dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>1.0-rc7</version> </dependency> public interface HelloService { void sayHello(); } import com.google.auto.service.AutoService; @AutoService(HelloService.class) public class EnglishHelloService implements HelloService { @Override public void sayHello() { System.out.println("Hello!"); } } <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-rc7</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> import java.util.ServiceLoader; public class Main { public static void main(String[] args) { ServiceLoader<HelloService> loader = ServiceLoader.load(HelloService.class); for (HelloService helloService : loader) { helloService.sayHello(); } } }


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