public interface GreetingService { String greet(); } @AutoService(GreetingService.class) public class EnglishGreetingService implements GreetingService { @Override public String greet() { return "Hello!"; } } @AutoService(GreetingService.class) public class ChineseGreetingService implements GreetingService { @Override public String greet() { } } <dependencies> <dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>1.0-rc7</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.8.0</version> <configuration> <source>1.8</source> <target>1.8</target> <annotationProcessorPaths> <path> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>1.0-rc7</version> </path> </annotationProcessorPaths> </configuration> </plugin> </plugins> </build> ServiceLoader<GreetingService> loader = ServiceLoader.load(GreetingService.class); for (GreetingService service : loader) { System.out.println(service.greet()); }


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