Add provider#getService to locate the service implementation.

This commit is contained in:
wu-sheng 2017-10-27 14:21:30 +08:00
parent 8fdca1f4c4
commit fafd17f2ba
4 changed files with 17 additions and 2 deletions

View File

@ -116,4 +116,13 @@ public abstract class ModuleProvider {
throw new ServiceNotProvidedException("Provide more service implementations than Module requirements.");
}
}
public <T extends Service> T getService(Class<T> serviceType) throws ServiceNotProvidedException {
Service serviceImpl = services.get(serviceType);
if (serviceImpl != null) {
return (T)serviceImpl;
}
throw new ServiceNotProvidedException("Service " + serviceType.getName() + " should not be provided, based on module define.");
}
}

View File

@ -31,7 +31,7 @@ public class BaseModuleA extends Module {
}
public interface ServiceABusiness1 extends Service {
void print();
}
public interface ServiceABusiness2 extends Service {

View File

@ -50,6 +50,9 @@ public class ModuleAProvider extends ModuleProvider {
public class Business1 implements BaseModuleA.ServiceABusiness1 {
@Override public void print() {
System.out.println("ModuleA.Business1.print()");
}
}
public class Business2 implements BaseModuleA.ServiceABusiness2 {

View File

@ -25,7 +25,7 @@ import org.junit.Test;
*/
public class ModuleManagerTest {
@Test
public void testInit() throws ServiceNotProvidedException, ModuleNotFoundException, ProviderNotFoundException {
public void testInit() throws ServiceNotProvidedException, ModuleNotFoundException, ProviderNotFoundException, DuplicateProviderException {
ApplicationConfiguration configuration = new ApplicationConfiguration();
configuration.addModule("Test").addProviderConfiguration("TestModule-Provider", null);
configuration.addModule("BaseA").addProviderConfiguration("P-A",null);
@ -33,5 +33,8 @@ public class ModuleManagerTest {
ModuleManager manager = new ModuleManager();
manager.init(configuration);
BaseModuleA.ServiceABusiness1 serviceABusiness1 = manager.find("BaseA").provider().getService(BaseModuleA.ServiceABusiness1.class);
serviceABusiness1.print();
}
}