spring整合web

醉酒当歌 提交于 2019-11-27 05:55:13

0 导入jar包

1 tomcat启动加载配置文件

servlet-->init(ServletConfig)--><load-on-startup>2
filter-->init(FilterConfig)-->web.xml 注册过滤器自动调用
listener-->ServletContextListner-->servletContext对象监听
spring提供监听器 ContextLoaderListener -->web.xml <listener><listener-class>...
    如果只配置监听器,默认加载xml位置:/WEB-INF/applicationContext.xml

2 确定配置文件位置,通过系统初始化参数

ServletContext初始化参数 web.xml

<context-param>
    <param-name>contextConfigLocation
    <param-value>classpath:applicationContext.xml

xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
 
 <!-- 设置配置文件路径 -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
  
  <!-- 配置spring监听器,加载xml配置文件 -->
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
</web-app>

3从ServletContext作用域 获得spring容器(了解)

package com.itheima.web.servlet;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.itheima.service.AccountService;

/**
 * Servlet implementation class HellowServlet
 */
@WebServlet("/HellowServlet")
public class HellowServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public HellowServlet() {
        // TODO Auto-generated constructor stub
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        
        //从applicationContext作用于(ServletContext)获得spring容器
        //方式1:手动从作用域获取
        ApplicationContext applicationContext=
            (ApplicationContext) this.getServletContext().getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE);
        //方式2通过工具获取
        ApplicationContext applicationContext2=
                WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
        //操作
        AccountService accountService= (AccountService)applicationContext.getBean("accountService",AccountService.class);
        accountService.transfer("jack", "rose", 1000);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!