Suggested Pages

Monday, January 16, 2017

Service Provider Interface ( JAVA SPI) Example


Provider Interface


SimpleServiceProvider.java

package it.sample;

public interface SimpleServiceProvider{

     public void simpleService();
}


Provider Implementation


SimpleServiceProviderImpl.java

package it.sample.impl;

public class SimpleServiceProviderImpl interface SimpleServiceProvider{

     public void simpleService(){
    
  }
}


Provider Implementation Descriptor :


META-INF/services/it.sample.SimpleServiceProvider

it.sample.impl.SimpleServiceProviderImpl


Service Loader


ServiceLoader.java

import java.util.*;

package it.sample.client;

public class ServiceLoader {

     public Iterator loadSimpleServiceProvider(){
      ServiceLoader loader = ServiceLoader.load(SimpleServiceProvider.class);
      return loader.iterator();  

  }
}

No comments :

Post a Comment

Suggested Pages