dependencies { implementation 'com.google.auto.service:auto-service:1.0-rc6' annotationProcessor 'com.google.auto.service:auto-service:1.0-rc6' } <dependencies> <dependency> <groupId>com.google.auto.service</groupId> <artifactId>auto-service</artifactId> <version>1.0-rc6</version> </dependency> </dependencies> public interface ServiceInterface { void doSomething(); } import com.google.auto.service.AutoService; @AutoService(ServiceInterface.class) public class ServiceImplementation1 implements ServiceInterface { @Override public void doSomething() { System.out.println("ServiceImplementation1.doSomething()"); } } import java.lang.reflect.InvocationTargetException; import java.util.ServiceLoader; public class Main { public static void main(String[] args) throws NoSuchMethodException, InstantiationException, IllegalAccessException, InvocationTargetException { ServiceLoader<ServiceInterface> serviceLoader = ServiceLoader.load(ServiceInterface.class); for (ServiceInterface service : serviceLoader) { service.doSomething(); } } }


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