當前位置:編程學習大全網 - 源碼下載 - 如何使用spring的autowire為servlet註入bean

如何使用spring的autowire為servlet註入bean

在應用中壹般普通的JavaPojo都是由Spring來管理的,所以使用autowire註解來進行註入不會產生問題,但是有兩個東西是例外的,壹個是Filter,壹個是Servlet,這兩樣東西都是由Servlet容器來維護管理的,所以如果想和其他的Bean壹樣使用Autowire來註入的話,是需要做壹些額外的功夫的。

對於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>

  • 上一篇:夢見燈塔
  • 下一篇:通達信寫好的公式怎麽在條件選股公式裏面找不到?
  • copyright 2024編程學習大全網