Spring - Spring集成web环境 -基本三层架构搭建案例

文章目录

  • Spring集成web环境-基本三层架构搭建
    • 新建项目(案例)
      • 编写一些类和接口
      • 导入需要依赖
      • spring.xml和properties文件
      • 添加servlet、jsp、tomcat依赖
      • 模拟web层
        • 编写servlet
      • 成功测试运行
  • Spring集成web环境-ContextLoderListener监听器的分析
    • 监听器演示
      • 编写类实现监听器
      • 在web.xml配置监听器
      • 重新编写servlet
      • 成功测试运行
    • 解耦spring.xml
      • 撤除加载容器
      • 编写web.xml
      • 读取web.xml中全局参数
      • 成功测试运行
    • 解耦app对象1
      • 创建编写类
      • 编写Servlet
      • 成功测试运行
  • Spring提供获取应用上下文的工具
    • 解耦app对象2
      • 导入依赖
      • 编写web.xml
      • 编写servlet
      • 成功测试运行
      • 步骤分析

Spring集成web环境-基本三层架构搭建

新建项目(案例)

new project-maven-骨架-选择setting.xml路径

Spring - Spring集成web环境 -基本三层架构搭建案例_第1张图片

编写一些类和接口

Spring - Spring集成web环境 -基本三层架构搭建案例_第2张图片

导入需要依赖

   <dependencies>
     <dependency>
       <groupId>mysqlgroupId>
       <artifactId>mysql-connector-javaartifactId>
       <version>5.1.32version>
     dependency>

     <dependency>
       <groupId>c3p0groupId>
       <artifactId>c3p0artifactId>
       <version>0.9.1.2version>
     dependency>

     <dependency>
       <groupId>com.alibabagroupId>
       <artifactId>druidartifactId>
       <version>1.1.10version>
     dependency>

     <dependency>
       <groupId>junitgroupId>
       <artifactId>junitartifactId>
       <version>4.12version>
       <scope>testscope>
     dependency>

     <dependency>
       <groupId>org.springframeworkgroupId>
       <artifactId>spring-contextartifactId>
       <version>5.0.5.RELEASEversion>
     dependency>

     <dependency>
       <groupId>org.springframeworkgroupId>
       <artifactId>spring-testartifactId>
       <version>5.0.5.RELEASEversion>
     dependency>
         dependencies>

spring.xml和properties文件


<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd">


    <context:property-placeholder location="classpath:jdbc.properties">context:property-placeholder>
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.jdbc.Driver">property>
        <property name="jdbcUrl" value="jdbc:mysql://localhost:3306/mybatis">property>
        <property name="user" value="root">property>
        <property name="password" value="$12345">property>

    bean>


    <bean id="userDao" class="com.taotao.dao.impl.UserDaoImpl">bean>


    <bean id="userService" class="com.taotao.service.impl.UserServiceImpl">
        <property name="userDao" ref="userDao"/>
    bean>
beans>
jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/mybatis
jdbc.username=root
jdbc.password=12345

添加servlet、jsp、tomcat依赖

    <dependency>
      <groupId>javax.servletgroupId>
      <artifactId>javax.servlet-apiartifactId>
      <version>3.0.1version>
      <scope>providedscope>
    dependency>

    <dependency>
      <groupId>javax.servlet.jspgroupId>
      <artifactId>javax.servlet.jsp-apiartifactId>
      <version>2.2.1version>
      <scope>providedscope>
    dependency>


  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.tomcat.mavengroupId>
        <artifactId>tomcat7-maven-pluginartifactId>
        <version>2.2version>
      plugin>
    plugins>

  build>


模拟web层

编写servlet

webServlet = userServlet

package com.taotao.web;

import com.taotao.service.UserService;
import com.taotao.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

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 java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/14 20:25
 */
@SuppressWarnings({"all"})
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        UserServiceImpl userServiceImpl = app.getBean(UserServiceImpl.class);
        userServiceImpl.save();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

成功测试运行

Spring - Spring集成web环境 -基本三层架构搭建案例_第3张图片

Spring - Spring集成web环境 -基本三层架构搭建案例_第4张图片

Spring集成web环境-ContextLoderListener监听器的分析

Spring - Spring集成web环境 -基本三层架构搭建案例_第5张图片

监听器演示

编写类实现监听器

package com.taotao.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * create by 刘鸿涛
 * 2022/4/14 20:55
 */
@SuppressWarnings({"all"})
public class ContextLoaderListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //初始化Spring容器
        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        //将Spring的应用上下文对象存储到ServletContest域中
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("app",app);
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

在web.xml配置监听器

DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

<web-app>
  <display-name>Archetype Created Web Applicationdisplay-name>
  

  <listener>
    <listener-class>com.taotao.listener.ContextLoaderListenerlistener-class>
  listener>
web-app>

重新编写servlet

package com.taotao.web;

import com.taotao.service.UserService;
import com.taotao.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
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 java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/14 20:25
 */
@SuppressWarnings({"all"})
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
        ServletContext servletContext = this.getServletContext();
        //向上转型
        ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
        UserServiceImpl userServiceImpl = app.getBean(UserServiceImpl.class);
        userServiceImpl.save();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

成功测试运行

Spring - Spring集成web环境 -基本三层架构搭建案例_第6张图片

解耦spring.xml

撤除加载容器

package com.taotao.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * create by 刘鸿涛
 * 2022/4/14 20:55
 */
@SuppressWarnings({"all"})
public class ContextLoaderListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {
        //初始化Spring容器
        ApplicationContext app = new ClassPathXmlApplicationContext("");
        //将Spring的应用上下文对象存储到ServletContest域中
        ServletContext servletContext = servletContextEvent.getServletContext();
        servletContext.setAttribute("app",app);
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

编写web.xml


  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>applicationContext.xmlparam-value>
  context-param>

读取web.xml中全局参数

重新编写ContextLoaderListener.java

package com.taotao.listener;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

/**
 * create by 刘鸿涛
 * 2022/4/14 20:55
 */
@SuppressWarnings({"all"})
public class ContextLoaderListener implements ServletContextListener {

    public void contextInitialized(ServletContextEvent servletContextEvent) {

        ServletContext servletContext = servletContextEvent.getServletContext();

        //读取web.xml中的全局参数
        String contextConfigLocation = servletContext.getInitParameter("contextConfigLocation");
        //初始化Spring容器
        ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
        //将Spring的应用上下文对象存储到ServletContest域中

        servletContext.setAttribute("app",app);
    }

    public void contextDestroyed(ServletContextEvent servletContextEvent) {

    }
}

成功测试运行

Spring - Spring集成web环境 -基本三层架构搭建案例_第7张图片

解耦app对象1

创建编写类

 package com.taotao.listener;

import org.springframework.context.ApplicationContext;

import javax.servlet.ServletContext;

/**
 * create by 刘鸿涛
 * 2022/4/14 21:21
 */
@SuppressWarnings({"all"})
public class WebApplicationContextUtils {
    public static ApplicationContext getWebApplicationContext(ServletContext servletContext){
        return (ApplicationContext) servletContext.getAttribute("app");
    }
}

编写Servlet

package com.taotao.web;

import com.taotao.listener.WebApplicationContextUtils;
import com.taotao.service.UserService;
import com.taotao.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import javax.servlet.ServletContext;
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 java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/14 20:25
 */
@SuppressWarnings({"all"})
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext servletContext = this.getServletContext();
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserServiceImpl userServiceImpl = app.getBean(UserServiceImpl.class);
        userServiceImpl.save();
        System.out.println("Spring容器创建完毕....");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

成功测试运行

Spring - Spring集成web环境 -基本三层架构搭建案例_第8张图片

Spring提供获取应用上下文的工具

Spring - Spring集成web环境 -基本三层架构搭建案例_第9张图片

解耦app对象2

导入依赖

    <dependency>
      <groupId>org.springframeworkgroupId>
      <artifactId>spring-webartifactId>
      <version>5.0.5.RELEASEversion>
    dependency>

编写web.xml

重新配置监听器


  <context-param>
    <param-name>contextConfigLocationparam-name>
    <param-value>classpath:applicationContext.xmlparam-value>
  context-param>


  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
  listener>

编写servlet

主要导入类,不要冲突

Spring - Spring集成web环境 -基本三层架构搭建案例_第10张图片

package com.taotao.web;


import com.taotao.service.impl.UserServiceImpl;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletContext;
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 java.io.IOException;

/**
 * create by 刘鸿涛
 * 2022/4/14 20:25
 */
@SuppressWarnings({"all"})
@WebServlet("/userServlet")
public class UserServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

        ServletContext servletContext = this.getServletContext();
//        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        ApplicationContext app = WebApplicationContextUtils.getWebApplicationContext(servletContext);
        UserServiceImpl userServiceImpl = app.getBean(UserServiceImpl.class);
        userServiceImpl.save();
        System.out.println("Spring容器创建完毕....");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doGet(req, resp);
    }
}

成功测试运行

Spring - Spring集成web环境 -基本三层架构搭建案例_第11张图片

步骤分析

Spring - Spring集成web环境 -基本三层架构搭建案例_第12张图片

你可能感兴趣的:(笔记,成长阶段,java,spring,tomcat,maven)