Class DelegatingServletProxy
- All Implemented Interfaces:
Servlet,ServletConfig,Serializable
web.xml, specifying the name of the target bean in the Spring
application context.
This class was originally inspired by Spring's DelegatingFilterProxy
class and has the same general behavior.
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate StringThe name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateServletbean.private ServletTheHttpServletinstance that this proxy will delegate to and manage the lifecycle for.private ObjectObject used to syncrhonize access where needed.private static final longFor serialization.private StringThe name of the target bean in the Spring application context.private booleanWhether to invoke the Servlet lifecycle methods on the target bean.private WebApplicationContextThe application context from which the target filter will be retrieved. -
Constructor Summary
ConstructorsConstructorDescriptionConstructor for traditional use inweb.xml.Constructor using the givenServletdelegate.DelegatingServletProxy(String targetBeanName) Constructor that will retrieve the named target bean from the SpringWebApplicationContextfound in theServletContext(either the 'root' application context or the context named bysetContextAttribute(java.lang.String)).DelegatingServletProxy(String targetBeanName, WebApplicationContext wac) Constructor that will retrieve the named target bean from the given SpringWebApplicationContext. -
Method Summary
Modifier and TypeMethodDescriptionvoiddestroy()protected voiddestroyDelegate(Servlet del) Destroy the Servlet delegate.protected WebApplicationContextReturn theWebApplicationContextpassed in at construction time, if available.Return the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateServletbean.protected StringReturn the name of the target bean in the Spring application context.voidinit()protected ServletInitialize the Servlet delegate, defined as a bean in the given Spring application context.protected voidinvokeDelegate(Servlet del, ServletRequest request, ServletResponse response) Actually invoke the delegate Servlet with the given request and response.protected booleanReturn whether to invoke the Servlet lifecycle methods on the target bean.voidservice(ServletRequest request, ServletResponse response) voidsetContextAttribute(String name) Set the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateServletbean.voidsetTargetBeanName(String name) Set the name of the target bean in the Spring application context.voidsetTargetFilterLifecycle(boolean flag) Set whether to invoke theServlet.init(jakarta.servlet.ServletConfig)andServlet.destroy()lifecycle methods on the target bean.Methods inherited from class jakarta.servlet.GenericServlet
getInitParameter, getInitParameterNames, getServletConfig, getServletContext, getServletInfo, getServletName, init, log, log
-
Field Details
-
serialVersionUID
private static final long serialVersionUIDFor serialization.- See Also:
-
contextAttribute
The name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateServletbean. -
webApplicationContext
The application context from which the target filter will be retrieved. -
targetBeanName
The name of the target bean in the Spring application context. -
targetFilterLifecycle
private boolean targetFilterLifecycleWhether to invoke the Servlet lifecycle methods on the target bean. -
delegate
TheHttpServletinstance that this proxy will delegate to and manage the lifecycle for. -
delegateMonitor
Object used to syncrhonize access where needed.
-
-
Constructor Details
-
DelegatingServletProxy
public DelegatingServletProxy()Constructor for traditional use inweb.xml. -
DelegatingServletProxy
Constructor using the givenServletdelegate.Bypasses entirely the need for interacting with a Spring application context, specifying the target bean name, etc.
For use with instance-based registration of filters.
- Parameters:
del- theHttpServletinstance that this proxy will delegate to and manage the lifecycle for
-
DelegatingServletProxy
Constructor that will retrieve the named target bean from the SpringWebApplicationContextfound in theServletContext(either the 'root' application context or the context named bysetContextAttribute(java.lang.String)).For use with instance-based registration of filters.
The target bean must implement the standard
Servletinterface.- Parameters:
targetBeanName- name of the target servlet bean to look up in the Spring application context
-
DelegatingServletProxy
public DelegatingServletProxy(@Nonnull @NotEmpty String targetBeanName, @Nullable WebApplicationContext wac) Constructor that will retrieve the named target bean from the given SpringWebApplicationContext.For use with instance-based registration of filters.
The target bean must implement the standard
Servletinterface.The given
WebApplicationContextmay or may not be refreshed when passed in. If it has not, and if the context implementsConfigurableApplicationContext, arefresh()will be attempted before retrieving the named target bean.- Parameters:
targetBeanName- name of the target filter bean in the Spring application contextwac- the application context from which the target filter will be retrieved; ifnull, an application context will be looked up fromServletContextas a fallback
-
-
Method Details
-
setContextAttribute
Set the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateServletbean.- Parameters:
name- of attribute
-
getContextAttribute
Return the name of the ServletContext attribute which should be used to retrieve theWebApplicationContextfrom which to load the delegateServletbean.- Returns:
- name of attribute
-
setTargetBeanName
Set the name of the target bean in the Spring application context.The target bean must implement the standard
Servletinterface.By default, the
servlet-nameas specified for the DelegatingFilterProxy inweb.xmlwill be used.- Parameters:
name- bean name
-
getTargetBeanName
Return the name of the target bean in the Spring application context.- Returns:
- bean name
-
setTargetFilterLifecycle
public void setTargetFilterLifecycle(boolean flag) Set whether to invoke theServlet.init(jakarta.servlet.ServletConfig)andServlet.destroy()lifecycle methods on the target bean.Default is "false"; target beans usually rely on the Spring application context for managing their lifecycle. Setting this flag to "true" means that the servlet container will control the lifecycle of the target bean, with this proxy delegating the corresponding calls.
- Parameters:
flag- flag to set
-
isTargetFilterLifecycle
protected boolean isTargetFilterLifecycle()Return whether to invoke the Servlet lifecycle methods on the target bean.- Returns:
- whether to invoke the Servlet lifecycle methods on the target bean
-
init
- Overrides:
initin classGenericServlet- Throws:
ServletException
-
service
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException - Specified by:
servicein interfaceServlet- Specified by:
servicein classGenericServlet- Throws:
ServletExceptionIOException
-
destroy
public void destroy()- Specified by:
destroyin interfaceServlet- Overrides:
destroyin classGenericServlet
-
findWebApplicationContext
Return theWebApplicationContextpassed in at construction time, if available.Otherwise, attempt to retrieve a
WebApplicationContextfrom theServletContextattribute with the configured name if set. Otherwise look up aWebApplicationContextunder the well-known "root" application context attribute.The
WebApplicationContextmust have already been loaded and stored in theServletContextbefore this filter gets initialized (or invoked).Subclasses may override this method to provide a different
WebApplicationContextretrieval strategy.- Returns:
- the
WebApplicationContextfor this proxy, ornullif not found
-
initDelegate
Initialize the Servlet delegate, defined as a bean in the given Spring application context.The default implementation fetches the bean from the application context and calls the standard
Servlet.initmethod on it, passing in the ServletConfig of this Servlet proxy.- Parameters:
wac- the root application context- Returns:
- the initialized delegate Filter
- Throws:
ServletException- if thrown by the servlet or if no bean name can be found
-
invokeDelegate
protected void invokeDelegate(@Nonnull Servlet del, @Nonnull ServletRequest request, @Nonnull ServletResponse response) throws ServletException, IOException Actually invoke the delegate Servlet with the given request and response.- Parameters:
del- the delegate Servletrequest- the current requestresponse- the current response- Throws:
ServletException- if thrown by the ServletIOException- if thrown by the Servlet
-
destroyDelegate
Destroy the Servlet delegate.- Parameters:
del- the Servlet delegate
-