提交 fafd17f2 编写于 作者: wu-sheng's avatar wu-sheng

Add provider#getService to locate the service implementation.

上级 8fdca1f4
......@@ -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.");
}
}
......@@ -31,7 +31,7 @@ public class BaseModuleA extends Module {
}
public interface ServiceABusiness1 extends Service {
void print();
}
public interface ServiceABusiness2 extends Service {
......
......@@ -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 {
......
......@@ -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();
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册