mirror of
https://github.com/google/guice.git
synced 2024-04-21 12:32:36 +00:00
Page:
ServletExtensionSPI
Pages
3rdPartyModules
AOP
AppsThatUseGuice
AssistedInject
Avoid Injecting Closable Resources
AvoidCallingProvideMethodsAndInjectConstructors
AvoidConditionalLogicInModules
AvoidStaticState
BeCarefulAboutIoInProviders
BestPractices
BindingAnnotations
BindingResolution
Bindings
Bootstrap
BoundFields
BuiltInBindings
ClassLoading
CustomInjections
CustomScopes
CyclicDependencies
DocumentPublicBindings
DontReuseAnnotations
Errors
ExtendingGuice
ExtensionSPI
ExternalDocumentation
FrequentlyAskedQuestions
GettingStarted
GoogleAppEngine
Grapher
Guice10
Guice20
Guice30
Guice40
Guice41
Guice42
Guice421
Guice422
Guice423
Guice500
Guice501
Guice510
Guice600
Guice700
GuiceDiscussions
GuiceInKotlin
GuicePersist
GuicePersistMultiModules
Home
InjectOnlyDirectDependencies
InjectingProviders
InjectingTheInjector
InjectionPoints
Injections
InspectingModules
InstanceBindings
JPA
JSR330
JustInTimeBindings
KeepConstructorsHidden
LinkedBindings
MentalModel
MinimizeMutability
ModulesShouldBeFastAndSideEffectFree
Motivation
Multibindings
OSGi
OptionalAOP
OrganizeModulesByFeature
PreferAtProvides
ProviderBindings
ProvidesMethods
RestrictedBindingSource
Scopes
ServletExtensionSPI
ServletModule
ServletRegexKeyMapping
Servlets
SpringComparison
Struts2Integration
ThrowingProviders
ToConstructorBindings
Transactions
UntargettedBindings
UseNullable
Clone
6
ServletExtensionSPI
Michael Diamond edited this page 2020-01-17 18:32:26 -08:00
Table of Contents
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.
-
User's Guide
-
Integration
-
Extensions
-
Internals
-
Releases
-
Community