使用Spring的autowire为简单Servlet注入

使用Spring的autowire为简单Servlet注入的两种方法,而非过滤Servlet和监听Servlet;本文的Servlet是调用service层,然后service层调用dao层保存一个User对象。保存User对象时并没有真正保存在数据库中,只是简单输出一句话而已。Spring使用的是xml方式,非annotation方式。

方法一:

步骤一:编写Spring的配置文件,写上要注入到Servlet中的service层对象

<bean id="userService" class="org.jgao.service.impl.UserServiceImpl" autowire="byName"/>

步骤二:在web.xml文件中配置Spring容器的初始化

<listener>
  	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  <context-param>
  	<param-name>contextConfigLocation</param-name>
  	<param-value>classpath:beans.xml</param-value>
  </context-param>

步骤三:编写Servlet类

package org.jgao.servlet;

import java.io.IOException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jgao.model.User;
import org.jgao.service.IUserService;
import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.userService.addUser(new User());
	}
	public void init(ServletConfig config) throws ServletException {//关键在此
  		ServletContext servletContext = config.getServletContext();
  		WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
  		AutowireCapableBeanFactory factory = webApplicationContext.getAutowireCapableBeanFactory();
  		factory.configureBean(this, "userService");//和Spring配置文件中的bean名字一样,即要注入的bean名字
 	}
}

步骤四:在web.xml文件中配置Servlet的映射路径

<servlet>
    <servlet-name>userServlet</servlet-name>
    <servlet-class>org.jgao.servlet.UserServlet</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>userServlet</servlet-name>
    <url-pattern>/userServlet</url-pattern>
  </servlet-mapping>

以上方式,只要bean名字和init方法中注入的名字一样就可以注入,但是个人测试:这样的注入,在Servlet类中有一个或多个的service层对象,每个都会注入进来。

方法二:真正执行的Servlet通过一个中间Servlet操作,再通过这个中间Servlet从Spring中取得真正执行的Servlet,让Spring管理Servlet,好像有些类似于代理。

步骤一:编写真正执行的Servlet,与方法一中的Servlet差不多,只是不用init初始化方法了

package org.jgao.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jgao.model.User;
import org.jgao.service.IUserService;

public class UserServlet extends HttpServlet {

	private static final long serialVersionUID = 1L;
	
	private IUserService userService;
	
	public IUserService getUserService() {
		return userService;
	}
	public void setUserService(IUserService userService) {
		this.userService = userService;
	}

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.userService.addUser(new User());
	}

步骤二:在Spring的配置文件中不仅配置要注入到Servlet中的bean,还配置真正执行的Servlet的bean

<bean id="userService" class="org.jgao.service.impl.UserServiceImpl" autowire="byName"/>
<bean id="userServlet" class="org.jgao.servlet.UserServlet" autowire="byName"></bean>

步骤三:在web.xml文件中配置Spring容器的初始化,与方法一中的一样。
步骤四:编写中间Servlet,在此类中,保存着真正执行的Servlet对象,从Spring中取得

package org.jgao.servletproxy;

import java.io.IOException;

import javax.servlet.GenericServlet;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class DelegatingServletProxy extends GenericServlet {

	private static final long serialVersionUID = 1L;
	
	private String proxyServletName;//配置在web.xml文件中的被代理的Servlet的名字
	private Servlet proxyServletObj;//被代理的Servlet对象
	
	@Override
	public void service(ServletRequest request, ServletResponse response)
			throws ServletException, IOException {
		this.proxyServletObj.service(request, response);//执行请求
	}

	@Override
	public void init() throws ServletException {
		this.proxyServletName = this.getServletName();//拿到Servlet名字
		this.proxyServletObj = this.getProxyServletBean();
		this.proxyServletObj.init(getServletConfig());
	}
	private Servlet getProxyServletBean() {
		WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
		return (Servlet)wac.getBean(this.proxyServletName);//根据名字拿到bean,转换成一个Servlet对象
	}	
}

步骤五:配置Servlet的映射路径,跟方法一中不同

  <servlet>
    <servlet-name>userServlet</servlet-name>
    <servlet-class>org.jgao.servletproxy.DelegatingServletProxy</servlet-class>
  </servlet>

  <servlet-mapping>
    <servlet-name>userServlet</servlet-name>
    <url-pattern>/userServlet</url-pattern>
  </servlet-mapping>

以上就是关于使用Spring的autowire为简单Servlet注入的两种方式的关键说明与关键代码演示,关于所有代码,我还不知道怎么传上来,如有需要的,请留下邮箱!

最后,小弟才疏学浅,第一次发表文章,难免有疏漏,还请海涵,不足之处或错误之处,还请各位大虾不吝赐教!!!微笑

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值