[转]Spring容器启动后自动执行Servlet进行预处理
- -通常做法是定义一个Servlet,并在web.xml中配置Servlet的启动顺序的值在DispatcherServlet之后. 但这样做的缺点是在Servlet中无法使用Spring的依赖注入功能,只能使用WebApplicationContext的getBean()方法获取bean.
通常做法是定义一个Servlet,并在web.xml中配置Servlet的启动顺序<load-on-startup>的值在DispatcherServlet之后。但这样做的缺点是在Servlet中无法使用Spring的依赖注入功能,只能使用WebApplicationContext的getBean()方法获取bean。
package com.xxxx;
import java.io.IOException;
import javax.servlet.Servlet;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
public class DelegatingServletProxy extends HttpServlet {
private static final long serialVersionUID = 1L;
private String targetServletBean;
private Servlet proxy;
@Override
public void init() throws ServletException {
this.targetServletBean = this.getInitParameter("targetServletBean");
this.getServletBean();
this.proxy.init(this.getServletConfig());
}
@Override
protected void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
proxy.service(request,response);
}
private void getServletBean(){
ServletContext servletContext = this.getServletContext();
WebApplicationContext wac = null;
wac = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
this.proxy = (Servlet) wac.getBean(targetServletBean);
}
} <servlet>
<servlet-name>proxyServletBean</servlet-name>
<servlet-class>com.xxxx.DelegatingServletProxy</servlet-class>
<init-param>
<param-name>targetServletBean</param-name>
<param-value>myBean</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>proxyServletBean</servlet-name>
<url-pattern>/file</url-pattern>
</servlet-mapping> @Component("myBean")
public class MyServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Resource
private UserService userService;
/**
* @see HttpServlet#HttpServlet()
*/
public InitialServlet() {
super();
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
}
@Override
public void init(ServletConfig config) throws ServletException {
//初始化eserMap
List<User> users = userService.getUsers();
for(int i = 0; i < users.size(); i++) {
User user = users.get(i);
Integer userId = user.getUserId();
String userName = user.getUserName();
SysCode.userMap.put(userId, userName);
}
}
}
<servlet>
<servlet-name>proxyServletBean2</servlet-name>
<servlet-class>com.xxxx.DelegatingServletProxy</servlet-class>
<init-param>
<param-name>targetServletBean</param-name>
<param-value>myBean2</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>proxyServletBean2</servlet-name>
<url-pattern>/file2</url-pattern>
</servlet-mapping>
<servlet>
<servlet-name>proxyServletBean3</servlet-name>
<servlet-class>com.xxxx.DelegatingServletProxy</servlet-class>
<init-param>
<param-name>targetServletBean</param-name>
<param-value>myBean3</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>proxyServletBean3</servlet-name>
<url-pattern>/WebService</url-pattern>
</servlet-mapping>