Spring整合Servlet
来源:互联网 发布:外汇软件mt4骗局 编辑:程序博客网 时间:2024/06/03 12:52
第一步:在web.xml中注册Spring的监听器
这时我们在Web项目中添加Spring的第一步(当然肯定要添加Spring所需要的Jar包啦)
- <span style="font-size: 16px;"><?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- 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-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app></span>
第二步:编写一个管理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;
- @SuppressWarnings("serial")
- /**
- * 我们自己实现的一个代理类用于将Servlet转为Spring管理的Servlet Bean
- */
- public class ServletToBeanProxy extends GenericServlet {
- private String targetBean;//当前客户端请求的Servlet名字
- private Servlet proxy;//代理Servlet
- @Override
- public void init() throws ServletException {
- super.init();
- WebApplicationContext wac =
- WebApplicationContextUtils.getRequiredWebApplicationContext(getServletContext()); //初始化Spring容器
- this.targetBean = getServletName();
- this.proxy = (Servlet) wac.getBean(targetBean);//调用ServletBean
- proxy.init(getServletConfig());//调用初始化方法将ServletConfig传给Bean
- }
- @Override
- public void service(ServletRequest arg0, ServletResponse arg1)
- throws ServletException, IOException {
- proxy.service(arg0, arg1);//在service方法中调用bean的service方法,servlet会根据客户的请求去调用相应的请求方法(Get/Post)
- }
- }
第二步:在web.xml中注册Servlet
注意这里的Servlet是有规范的一定要遵循
1、<servlet-name>的名字必须是Spring容器中ServletBean的id
2、<servlet-class>必须是上面写的代理类的全路径com.xwl.estore.servlet.ServletToBeanProxy
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5"
- xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
- http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
- <!-- 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>
- </servlet-mapping>
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- </welcome-file-list>
- </web-app>
第三步:编写ServletBean(这也是一个Servlet要继承HttpServlet)
- 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;
- @SuppressWarnings("serial")
- @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 {
- System.out.println("Get");
- PrintWriter out = response.getWriter();
- out.println(userService.sayHello("Hello,Spring.Servlet"));
- }
- public void doPost(HttpServletRequest request, HttpServletResponse response)
- throws ServletException, IOException {
- System.out.println("Post");
- PrintWriter out = response.getWriter();
- out.println(userService.sayHello("Hello,Spring.Servlet"));
- }
- }
第四步:编写Spring配置文件(applicationContext.xml)
- <span style="font-size: 16px;">这里我用的是annotation的方式</span>
- <span style="font-size: 16px;"><?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></span>
第五步:进行测试
为了测试我编写了一个Service类(一个接口和一个实现)
下面是Service接口
- package com.xwl.estore.service;
- public interface UserService {
- public String sayHello(String hello);
- }
下面是Service接口的实现
- 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;
- }
- }
在浏览器中输入Url http://localhost:8080/EStore/HelloServlet 进行访问结果如下:
Hello,Spring.Servlet
转载码龙008博客http://blog.csdn.net/xwl617756974/article/details/7451773
- 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
- hdu 1251 统计难题 (字典树)
- “易信”亮点功能的测试报告
- <<git使用快速教程>>
- 财付通接口
- 媒体平台 Media Foundation
- Spring整合Servlet
- Unity3d 4.0新动画系统Mecanim初探(一)
- 自己实现 STL 迭代器
- URL相关操作
- 硬件检测:安装、停用
- linux学习笔记总结
- 解读SDN核心技术:OpenFlow深入分析
- HDFS海量存储
- 'SVN 提交' attemped to lock an already-locked dir svn