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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值