@Documented
@Retention(value=RUNTIME)
@Target(value=TYPE)
@Inherited
public @interface DependsOn
This annotation is only effective when present on Service
types.
import ratpack.server.ServerConfig;
import ratpack.server.RatpackServer;
import ratpack.service.DependsOn;
import ratpack.service.Service;
import ratpack.service.StartEvent;
import ratpack.service.StopEvent;
import java.util.ArrayList;
import java.util.List;
import static java.util.Arrays.asList;
import static org.junit.Assert.assertEquals;
public class Example {
private static final List<String> EVENTS = new ArrayList<>();
private static class ServiceOne implements Service {
@Override
public void onStart(StartEvent event) throws Exception {
EVENTS.add("one-start");
}
@Override
public void onStop(StopEvent event) throws Exception {
EVENTS.add("one-stop");
}
}
@DependsOn(ServiceOne.class)
private static class ServiceTwo implements Service {
@Override
public void onStart(StartEvent event) throws Exception {
EVENTS.add("two-start");
}
@Override
public void onStop(StopEvent event) throws Exception {
EVENTS.add("two-stop");
}
}
public static void main(String[] args) throws Exception {
RatpackServer server = RatpackServer.of(s -> s
.serverConfig(ServerConfig.embedded())
.registryOf(r -> r
// note: order of registration is irrelevant here
.add(new ServiceOne())
.add(new ServiceTwo())
)
);
server.start();
assertEquals(asList("one-start", "two-start"), EVENTS);
server.stop();
assertEquals(asList("one-start", "two-start", "two-stop", "one-stop"), EVENTS);
}
}
ServiceDependencies
Modifier and Type | Required Element and Description |
---|---|
java.lang.Class<?>[] |
value
The types of services that services of the annotated type depend on.
|