Spring - Spring集成web环境 -基本三层架构搭建案例
Posted 鬼鬼骑士
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Spring - Spring集成web环境 -基本三层架构搭建案例相关的知识,希望对你有一定的参考价值。
文章目录
Spring集成web环境-基本三层架构搭建
新建项目(案例)
new project-maven-骨架-选择setting.xml路径
编写一些类和接口
导入需要依赖
<dependencies>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.32</version>
</dependency>
<dependency>
<groupId>c3p0</groupId>
<artifactId>c3p0</artifactId>
<version>0.9.1.2</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
</dependencies>
spring.xml和properties文件
<?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: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">
<!-- 加载外部的properties文件-->
<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>
<!-- 配置Dao-->
<bean id="userDao" class="com.taotao.dao.impl.UserDaoImpl"></bean>
<!-- 配置service-->
<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.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>javax.servlet.jsp-api</artifactId>
<version>2.2.1</version>
<scope>provided</scope>
</dependency>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
</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集成web环境-ContextLoderListener监听器的分析
监听器演示
编写类实现监听器
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 Application</display-name>
<!-- 配置监听器-->
<listener>
<listener-class>com.taotao.listener.ContextLoaderListener</listener-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.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>contextConfigLocation</param-name>
<param-value>applicationContext.xml</param-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)
成功测试运行
解耦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
Spring集成web环境
ApplicationContext应用上下文获取方式
应用上下文对象是通过new ClasspathXmlApplicationContext(spring配置文件) 方式获取的,但是每次从容器中获得Bean时都要编写new ClasspathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。
在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Spring的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。
Spring提供获取应用上下文的工具
Spring提供了一个监听器ContextLoaderListener就是对上述功能的封装,该监听器内部加载Spring配置文件,创建应用上下文对象,并存储到ServletContext域中,提供了一个客户端工具WebApplicationContextUtils供使用者获得应用上下文对象。
所以我们需要做的只有两件事:
1在web.xml中配置ContextLoaderListener监听器(导入spring-web坐标)
2使用WebApplicationContextUtils获得应用上下文对象ApplicationContext
导入Spring集成web的坐标
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.5.RELEASE</version>
</dependency>
配置ContextLoaderListener监听器
<!--全局参数-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!--Spring的监听器-->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
通过工具获得应用上下文对象
//在servlet 中
ServletContext servletContext = this.getServletContext();
ApplicationContext applicationContext =WebApplicationContextUtils.getWebApplicationContext(servletContext);
Object obj = applicationContext.getBean("id");//根据模块