Web and Servlets
Inspecting Servlet Bindings
登录后可跨设备保存划线和私人笔记登录
Using the servlet module SPI
ServletModule Extension SPI
New in Guice 3.0
Sometimes you want to examine your Modules and/or Bindings to inspect which URLs are serving which servlets or filters. This is useful for tests or to assert preconditions in your code. The !ServletModule extension SPI, built on the core extension SPI, lets you do this the same way you would inspect a binding using the normal elements SPI.
Inspecting Servlet Bindings
ServletModuleTargetVisitor is an extension to the core BindingTargetVisitor. You can implement this interface and call binding.acceptTargetVisitor(myVisitor) to learn details about servlet bindings.
boolean isBindingForUri(Binding<?> binding, String uri) {
return binding.acceptTargetVisitor(new Visitor(uri));
}
private static class Visitor
extends DefaultBindingTargetVisitor<Object, Boolean>
implements ServletModuleTargetVisitor<Object, Boolean> {
private final String uri;
Visitor(String uri) {
this.uri = uri;
}
@Override boolean visitOther(Binding<?> binding) {
return false;
}
@Override boolean visit(InstanceFilterBinding binding) {
return matchesUri(binding);
}
@Override boolean visit(InstanceServletBinding binding) {
return matchesUri(binding);
}
@Override boolean visit(LinkedFilterBinding binding) {
return matchesUri(binding);
}
@Override boolean visit(LinkedServletBinding binding) {
return matchesUri(binding);
}
private boolean matchesUri(ServletModuleBinding binding) {
return binding.matchesUri(uri);
}
}These visitors will work both on bindings retrieved from an Injector and bindings retrieved from Elements.
评论
登录后参与评论
正在加载评论…
KnowForge