Spring整合Servlet
来源:互联网 发布:剑三正太柔弱捏脸数据 编辑:程序博客网 时间:2024/06/05 17:10
在应用中一般普通的Java Pojo都是由Spring来管理的,所以使用autowire注解来进行注入不会产生问题,但是有两个东西是例外的,一个是Filter,一个是Servlet,这两样东西都是由Servlet容器来维护管理的,所以若想和其他的Bean一样使用Autowire来注入的话,是需要做一些额外的功夫的。
第1步:在web.xml中注册Spring的监听器
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
第2步:编写一个管理Servlet的类
这个类主要是将Servlet和Spring中的Bean结合起来,方便Spring对Servlet进行管理,起到一个中转的作用
package com.xwl.estore.servlet;
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;
/**
* 将Servlet转为Spring管理的Servlet Bean
*/
public class ServletToBeanProxy extends GenericServlet {
// 当前客户端请求的Servlet名字
private String targetBean;
// 代理Servlet
private Servlet proxy;
@Override
public void init() throws ServletException {
super.init();
// 初始化Spring容器
WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext());
// 获取Servlet名
this.targetBean = getServletName();
// 调用ServletBean
this.proxy = (Servlet) wac.getBean(targetBean);
// 调用初始化方法将ServletConfig传给Bean
proxy.init(getServletConfig());
}
@Override
public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException {
// 在service方法中调用bean的service方法,servlet会根据客户的请求去调用相应的请求方法(Get/Post)
proxy.service(request, response);
}
}
第3步:在web.xml中注册Servlet
<servlet-name>的名字必须是Spring容器中ServletBean的id
<servlet-class>必须是上面写的代理类的全路径com.xwl.estore.servlet.ServletToBeanProxy
<!-- Spring -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/classes/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!-- Servlet -->
<servlet>
<servlet-name>helloServlet</servlet-name>
<servlet-class>com.xwl.estore.servlet.ServletToBeanProxy</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>helloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
第4步:编写ServletBean
package com.xwl.estore.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.annotation.Resource;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.xwl.estore.service.UserService;
@Controller
@Scope("prototype")
public class HelloServlet extends HttpServlet {
private UserService userService;
@Resource
public void setUserService(UserService userService) {
this.userService = userService;
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(userService.sayHello("Hello,Spring.Servlet"));
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
PrintWriter out = response.getWriter();
out.println(userService.sayHello("Hello,Spring.Servlet"));
}
}
第5步:编写Spring配置文件(applicationContext.xml)
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
<context:annotation-config></context:annotation-config>
<context:component-scan base-package="com.xwl.estore"></context:component-scan>
</beans>
第6步:进行测试
package com.xwl.estore.service;
public interface UserService {
public String sayHello(String hello);
}
package com.xwl.estore.service.impl;
import org.springframework.stereotype.Service;
import com.xwl.estore.service.UserService;
@Service
public class UserServiceImpl implements UserService {
public String sayHello(String hello) {
return hello;
}
}
原帖地址:
http://blog.csdn.net/xwl617756974/article/details/7451773
http://akhuting.iteye.com/blog/904697
- spring整合servlet
- Spring整合Servlet
- Spring整合Servlet
- Spring整合Servlet
- Spring 与 Servlet 整合
- spring 怎么连接servlet,spring整合servlet,spring管理servlet
- dwr与servlet spring 整合
- Spring和Servlet的整合
- xfire整合spring时引入的servlet
- Spring简单整合servlet和mybatis
- Spring+Servlet整合(如何向Servlet注入属性)
- Spring学习笔记(1)----将Spring与Servlet整合
- Spring+Servlet整合(如何向Servlet注入属性(转),servlet获取spring容器中的bean)
- Struts2、Hibernate、Spring整合时如何向Servlet注入属性
- Struts2、Hibernate、Spring整合时如何向Servlet注入属性
- Hibernate+Spring+Servlet整合开发中的分页显示方案
- Jsp/Servlet整合Spring原理及源码分析
- Spring Boot整合模板引擎freemarker以及servlet
- git clone 简介
- 新浪微博sdk错误---The target server failed to respond
- 程序输出源文件本身(面试题)
- lsof使用技巧
- SQL数据库备份还原语句
- Spring整合Servlet
- 谁是你生命中最重要的人
- slat cmd
- adoconnection连接数据库,长时间不用,连接失败
- Apache HTTP 服务器目录映射到本地目录
- redis:Redis server went away报错解决
- 韩寒的经典语录
- 程序员学习能力提升三要素
- 如何正确使用flash player的新功能以及flashdevelop编辑器相关知识