How Service Discovery and Dependency Injection work
developARouter supports decoupled API calls via Service Discovery.
- Expose a Service: Define an interface that extends
IProviderand implement it in a class annotated with@Route. - Discover a Service: Use
@Autowiredon a field to inject the service by type, or useARouter.getInstance().navigation(ServiceClass.class)to find it by class.
Note: If multiple implementations of the same interface exist, you must use @Autowired(name = "/path/to/service") to specify which one to inject (by name).
// 1. Define and Implement Service
public interface HelloService extends IProvider {
String sayHello(String name);
}
@Route(path = "/yourservicegroupname/hello")
public class HelloServiceImpl implements HelloService {
@Override
public String sayHello(String name) { return "hello, " + name; }
@Override
public void init(Context context) {}
}
// 2. Discover Service
public class Test {
@Autowired
HelloService helloService; // Injected by type
@Autowired(name = "/yourservicegroupname/hello")
HelloService helloService2; // Injected by name
public void test() {
ARouter.getInstance().inject(this);
helloService.sayHello("Vergil");
}
}