大家知道,儅你用Spring管理bean時,在自己定義的過濾器中是不能通過Spring取到bean的,必須自己手動取,現將我笨拙的方法寫下來供大家參考
首先在web.xml中加入如下代碼
具體實現如下:
首先在web.xml中加入如下代碼
<servlet>
<servlet-name>GetSpringContext</servlet-name>
<servlet-class>com.morris.GetSpringContext</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
具體實現如下:
public class GetSpringContext extends HttpServlet {
/**
* @author Morris
*/
private static final long serialVersionUID = 1L;
private static Log logger = LogFactory.getLog(GetSpringContext.class);
public static commonService commonService = null;
public GetSpringContext(){};
public void init(){
WebApplicationContext ctx =WebApplicationContextUtils.getWebApplicationContext(getServletContext());
commonService = (commonService)ctx.getBean("commonService"); //自己隨便取啦
}
public void doGet(HttpServletRequest request, HttpServletResponse response) {
}