Sometimes in a spring (3.2.5) project, if you use a servlet, you might want the servlet instance to be managed as a bean by the spring container, which also automatically injects the other needed beans, checking The discovery only provides the proxy class Org.springframework.web.filter.DelegatingFilterProxy for the filter and does not provide a proxy class for the servlet, so it mimics the following proxy class:
Package org.springframework.web.servlet;
Import java.io.IOException;
Import javax.servlet.Servlet;
Import javax.servlet.ServletException;
Import javax.servlet.ServletRequest;
Import javax.servlet.ServletResponse;
Import org.springframework.context.ConfigurableApplicationContext;
Import org.springframework.util.Assert;
Import org.springframework.web.context.WebApplicationContext;
Import org.springframework.web.context.support.WebApplicationContextUtils;
/**
* Servlet proxy class that hosts the servlet in the spring container to automatically inject other bean<br> directly inside the servlet
* Reference {@code Org.springframework.web.filter.delegatingfilterproxy}<br>
*/
@SuppressWarnings ("serial")
public class Delegatingservletproxy extends Httpservletbean {
Private String contextattribute;
Private Webapplicationcontext webapplicationcontext;
Private String targetbeanname;
Private Boolean targetservletlifecycle = true;
Private volatile Servlet delegate;
Private Final Object delegatemonitor = new Object ();
Public Delegatingservletproxy () {
}
Public Delegatingservletproxy (Servlet Delegate) {
Assert.notnull (delegate, "delegate Servlet object must not being null");
This.delegate = delegate;
}
Public Delegatingservletproxy (String Targetbeanname) {
This (targetbeanname, null);
}
Public delegatingservletproxy (String targetbeanname, webapplicationcontext Wac) {
Assert.hastext (targetbeanname, "target Servlet Bean name must not being null or empty");
This.settargetbeanname (targetbeanname);
This.webapplicationcontext = wac;
If (wac! = Null) {
This.setenvironment (wac.getenvironment ());
}
}
public void Setcontextattribute (String Contextattribute) {
This.contextattribute = contextattribute;
}
Public String Getcontextattribute () {
Return this.contextattribute;
}
public void Settargetbeanname (String Targetbeanname) {
This.targetbeanname = targetbeanname;
}
Protected String gettargetbeanname () {
Return this.targetbeanname;
}
public void Settargetservletlifecycle (boolean Targetservletlifecycle) {
This.targetservletlifecycle = targetservletlifecycle;
}
Protected Boolean istargetservletlifecycle () {
Return this.targetservletlifecycle;
}
@Override
protected void Initservletbean () throws servletexception {
Synchronized (this.delegatemonitor) {
if (this.delegate = = Null) {
if (this.targetbeanname = = Null) {
This.targetbeanname = This.getservletname ();
}
Webapplicationcontext WAC = This.findwebapplicationcontext ();
If (wac! = Null) {
This.delegate = This.initdelegate (wac);
}
}
}
}
@Override
public void service (servletrequest req, servletresponse Resp) throws servletexception, IOException {
Servlet Delegatetouse = this.delegate;
if (delegatetouse = = Null) {
Synchronized (this.delegatemonitor) {
if (this.delegate = = Null) {
Webapplicationcontext WAC = This.findwebapplicationcontext ();
if (WAC = = Null) {
throw new IllegalStateException ("No webapplicationcontext found:no contextloaderlistener registered?");
}
This.delegate = This.initdelegate (wac);
}
Delegatetouse = this.delegate;
}
}
This.invokedelegate (delegatetouse, req, resp);
}
@Override
public void Destroy () {
Servlet Delegatetouse = this.delegate;
If (delegatetouse! = Null) {
This.destroydelegate (delegatetouse);
}
}
Protected Webapplicationcontext Findwebapplicationcontext () {
If (this.webapplicationcontext! = Null) {
If (this.webapplicationcontext instanceof Configurableapplicationcontext) {
If (! ( (configurableapplicationcontext) this.webapplicationcontext). isActive ()) {
((configurableapplicationcontext) this.webapplicationcontext). Refresh ();
}
}
Return this.webapplicationcontext;
}
String attrname = This.getcontextattribute ();
If (attrname! = Null) {
return Webapplicationcontextutils.getwebapplicationcontext (super.getservletcontext (), attrname);
}
else {
return Webapplicationcontextutils.getwebapplicationcontext (super.getservletcontext ());
}
}
Protected Servlet initdelegate (webapplicationcontext Wac) throws servletexception {
Servlet delegate = Wac.getbean (this.gettargetbeanname (), servlet.class);
If (this.istargetservletlifecycle ()) {
Delegate.init (super.getservletconfig ());
}
Return delegate;
}
protected void Invokedelegate (Servlet delegate, servletrequest req, servletresponse Resp) throws servletexception, IOException {
Delegate.service (req, resp);
}
protected void Destroydelegate (Servlet Delegate) {
If (this.istargetservletlifecycle ()) {
Delegate.destroy ();
}
}
}
//======================================================//
Usage:
1, such as the existence of Test. testservlet, configure to spring corresponding xml:
<bean id= "testservlet" class= "test. Testservlet "/>
2. The Web. XML configuration is as Follows:
<servlet>
<servlet-name>testServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DelegatingServletProxy</servlet-class>
<init-param>
<param-name>targetBeanName</param-name>
<param-value>testServlet</param-value>
</init-param>
<load-on-startup>10</load-on-startup>
</servlet>
The required beans are then automatically injected into the Testservlet.
Integrate servlet to spring container