對於Filter,Spring提供了DelegatingFilterProxy,所以本文主要講述Servlet的解決。
1、比較直觀但是不大優雅的做法是重寫init()方法,在裏面使用AutowireCapableBeanFactory來手工告訴Spring:我這個Servlet是需要這樣的壹個Bean的。具體寫法:
public void init(ServletConfig servletConfig) throws ServletException {
ServletContext servletContext = servletConfig.getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
AutowireCapableBeanFactory autowireCapableBeanFactory = webApplicationContext.getAutowireCapableBeanFactory();
autowireCapableBeanFactory.configureBean(this, BEAN_NAME);
}
其中,BEAN_NAME就是需要註入的Bean在spring中註冊的名字.
這樣寫的主要問題是就是那個BEAN_NAME,這樣寫有點主動查找,而不是依賴註入的感覺。
2、創建壹個類似於DelegatingFilterProxy那樣的代理,通過代理根據配置來找到實際的Servlet,完成業務邏輯功能。
假定我們有壹個Servlet名字叫UserServlet,需要註入壹個UserManager,偽代碼如下:
public class UserServlet extends HttpServlet {
@Autowired(required = true)
private UserManager userManager;
}
第壹步:
public class DelegatingServletProxy extends GenericServlet {
private String targetBean;
private Servlet proxy;
@Override
public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException {
proxy.service(req, res);
}
@Override
public void init() throws ServletException {
this.targetBean = getServletName();
getServletBean();
proxy.init(getServletConfig());
}
private void getServletBean() {
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
this.proxy = (Servlet) wac.getBean(targetBean);
}
}
第二步:
配置web.xml文件,原來UserServlet的配置大致是這樣的:
<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>com.sample.UserServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>
現在修改為
<servlet>
<servlet-name>userServlet</servlet-name>
<servlet-class>com.sample.DelegatingServletProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>userServlet</servlet-name>
<url-pattern>/userServlet</url-pattern>
</servlet-mapping>