- 在实际开发中我们需要用到Spring和Servlet这样的组合(比如写一个小项目啥的)下面我来整理一下我我的整合:
第一步:在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);
- }
- 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;
- }
- }
Hello,Spring.Servlet
说明:Spring整合了Servlet之后我们就可以整合Hibernate,JDBC,和事务管理等,这里只是搭建搭建一个HelloWorld。