Servlet托管Spring进行管理

Servlet托管Spring时,
1、重写servlet中的init()方法,在servlet中使用WebApplicationContext 获取bean对象:
如下:
ArrivingShipsImpl asi;
public void init() throws ServletException {          
        super.init();
        ServletContext servletContext = this.getServletContext();  
        WebApplicationContext ctx =    WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
        asi = (ArrivingShipsImpl)ctx.getBean("arrivingShipsImpl");
    }


2、实现ApplicationContextAware接口的SpringConetextUtil类,利用回调方法,设置上下文对象,通过上下文对象获取bean对象

@Component
public class SpringContextUtil implements ApplicationContextAware{

	private static ApplicationContext	applicationContext;

	/**
	 * 实现ApplicationContextAware接口的回调方法,设置上下文环境
	 */
	public void setApplicationContext(ApplicationContext applicationContext){
		SpringContextUtil.applicationContext = applicationContext;
	}

	public static ApplicationContext getApplicationContext(){
		return applicationContext;
	}
	/**
	 * 获取对象
	 */
	public static Object getBean(String name) throws BeansException{
		return applicationContext.getBean(name);
	}
}



3、动态代理形式的:
http://blog.csdn.net/yaerfeng/article/details/7368541

思路:把servlet配置为spring的bean,就可以实现其他bean的注入,然后使用代理servlet来辅助配置和运行:

一、代理servlet的写法:

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
/**
 * HttpServlet 代理
 * @author lwei
 * @since 2011-03-17
 * @version 1.0
 */
public class HttpServletProxy extends HttpServlet {

  /**
   * random serialVersionUID
   */

  private static final long serialVersionUID = -7208519469035631940L;
  Log logger = LogFactory.getLog(HttpServletProxy.class);
  private String targetServlet;
  private HttpServlet proxy;
  public void init() throws ServletException {
      this.targetServlet = getInitParameter("targetServlet");
      getServletBean();
      proxy.init(getServletConfig());
      logger.info(targetServlet + " was inited by HttpServletProxy  successfully......");
  }

  private void getServletBean() {
      WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
      this.proxy = (HttpServlet) wac.getBean(targetServlet);
  }


  @Override
  public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, RuntimeException {
      proxy.service(request, response);      
 }



}

二、业务servlet的写法:
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
 * @author lwei
 */
public class UserCheckServlet extends HttpServlet {

  /**
   * random serialVersionUID
   */
  private static final long serialVersionUID = 3075635113536622929L;
  private UserService userService;(UserService 是spring托管的bean,通过set方法注入)
  public void setUserService (UserService userService) {
      this.userService = userService;
  }
  public UserCheckServlet() {
      super();
  }

  public void init() throws ServletException {
      super.init();
  }


  @Override
  public void service(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse)
       throws ServletException, IOException, RuntimeException {
     ....
     ....
     userService.getUserByCode();(注入后bean的使用)
     ....
     ....
 }   
}


三、业务serlvet配置为spring的bean:
<bean id="userCheckServlet" class="com.XXX.xxxxx.web.UserCheckServlet " />

四、web.xml中业务servlet的配置:
<servlet>
      <servlet-name>UserCheckProxy</servlet-name>
      <servlet-class>com.XXX.xxxx.web.HttpServletProxy</servlet-class>
     <init-param>
         <param-name>targetServlet</param-name>
         <param-value>userCheckServlet</param-value>(业务servlet配置为spring的bean时的名字)
     </init-param>
 </servlet>
 <servlet-mapping>
     <servlet-name>UserCheckProxy</servlet-name>
     <url-pattern>/UserCheck</url-pattern>
    </servlet-mapping>




猜你喜欢

转载自jadethao.iteye.com/blog/1768757