• Maven配置tomcat服务器和ApplicationContext应用上下文获取方法


    目录

    Maven配置tomcat服务器

    使用maven手动创建web工程

     ApplicationContext应用上下文获取方法

    web.xml下配置监听器

    优化代码

    解耦合1

    解耦合2


    Maven配置tomcat服务器

    首先这得是war包,如果这是普通包得添加webapp,并将pom.xml中加入如下,表示这是一个web文件

      <packaging>war</packaging>

    加完之后点击编辑配置

     其下的war二选一即可

     完成之后点击确认即可完成配置。

    使用maven手动创建web工程

    第一步,点击项目结构,选中 要添加的maven工程,右键添加web。

     第二步,点击web工程,修改web工程路径

     点击完成即可完成web工程的手动配置。

     ApplicationContext应用上下文获取方法

    应用上下文对象是通过new  ClasspathXmlApplicationContext(spring配置文件)方式获取的,但是每次从容器中获得Bean时都要编写new  ClasspathXmlApplicationContext(spring配置文件),这样的弊端是配置文件加载多次,应用上下文对象创建多次。

    在Web项目中,可以使用ServletContextListener监听Web应用的启动,我们可以在Web应用启动时,就加载Sprina的配置文件,创建应用上下文对象ApplicationContext,在将其存储到最大的域servletContext域中,这样就可以在任意位置从域中获得应用上下文ApplicationContext对象了。

    新建一个Listenter包下的ContextLoderListener

    1. package com.Listener;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. import javax.servlet.ServletContext;
    5. import javax.servlet.ServletContextEvent;
    6. import javax.servlet.ServletContextListener;
    7. public class ContextLoaderListener implements ServletContextListener {
    8. public void contextInitialized(ServletContextEvent servletContextEvent) {
    9. ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");
    10. //将Spring的应用上下文对象存储到ServletContext域中
    11. ServletContext servletContext = servletContextEvent.getServletContext();
    12. servletContext.setAttribute("app", app);
    13. System.out.println("Spring容器创建完毕");
    14. }
    15. public void contextDestroyed(ServletContextEvent servletContextEvent) {
    16. }
    17. }

    web包下

    1. package com.web;
    2. import com.service.*;
    3. import org.springframework.context.ApplicationContext;
    4. import javax.servlet.ServletContext;
    5. import javax.servlet.ServletException;
    6. import javax.servlet.annotation.WebServlet;
    7. import javax.servlet.http.HttpServlet;
    8. import javax.servlet.http.HttpServletRequest;
    9. import javax.servlet.http.HttpServletResponse;
    10. import java.io.IOException;
    11. @WebServlet("/user")
    12. public class UserServlet extends HttpServlet {
    13. @Override
    14. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    15. // ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
    16. ServletContext servletContext = req.getServletContext();
    17. ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
    18. UserService userService=app.getBean(UserService.class);
    19. }
    20. }

    web.xml下配置监听器

    1. <?xml version="1.0" encoding="UTF-8"?>
    2. <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
    5. version="4.0">
    6. <!--配置监听器-->
    7. <listener>
    8. <listener-class>com.Listener.ContextLoaderListener</listener-class>
    9. </listener>
    10. </web-app>

    自动创建spring容器

    优化代码

    解耦合1

    由于监听器内部我们将代码写固定了,不利于后期的维护操作,所以要解耦合,写在配置文件中进行解耦合。(“”引号内的名字任意)

    ApplicationContext app = new ClassPathXmlApplicationContext("applicationContext.xml");

    将此个代码写入到web.xml中进

    1. <!--全局初始化参数-->
    2. <context-param>
    3. <!--name和value为任意 -->
    4. <param-name>ContextConfigLocation</param-name>
    5. <param-value>applicationContext.xml</param-value>
    6. </context-param>

    在ContextLoaderListenter类中

    1. package com.Listener;
    2. import org.springframework.context.ApplicationContext;
    3. import org.springframework.context.support.ClassPathXmlApplicationContext;
    4. import javax.servlet.ServletContext;
    5. import javax.servlet.ServletContextEvent;
    6. import javax.servlet.ServletContextListener;
    7. public class ContextLoaderListener implements ServletContextListener {
    8. public void contextInitialized(ServletContextEvent servletContextEvent) {
    9. ServletContext servletContext = servletContextEvent.getServletContext();
    10. //读取web.xml中的全局参数
    11. String contextConfigLocation = servletContext.getInitParameter("ContextConfigLocation");
    12. ApplicationContext app = new ClassPathXmlApplicationContext(contextConfigLocation);
    13. //将Spring的应用上下文对象存储到ServletContext域中
    14. servletContext.setAttribute("app", app);
    15. System.out.println("Spring容器创建完毕");
    16. }
    17. }

    读取配置文件的值,这样就完成了解耦合

    解耦合2

    在userServlet类中 

    ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");

    这样耦合了app代码,让这个只能叫做app,这样不利于后期维护和辨认。

    所以我们这么改,在Listener包下创建一个工具类WebApplicationContextUtils

    1. package com.Listener;
    2. import org.springframework.context.ApplicationContext;
    3. import javax.servlet.ServletContext;
    4. public class WebApplicationContextUtils {
    5. public static ApplicationContext getApplicationContext(ServletContext servletContext){
    6. return (ApplicationContext) servletContext.getAttribute("app");
    7. }
    8. }

    在userServlet处代码修改为

    1. package com.web;
    2. import com.Listener.WebApplicationContextUtils;
    3. import com.service.*;
    4. import org.springframework.context.ApplicationContext;
    5. import javax.servlet.ServletContext;
    6. import javax.servlet.ServletException;
    7. import javax.servlet.annotation.WebServlet;
    8. import javax.servlet.http.HttpServlet;
    9. import javax.servlet.http.HttpServletRequest;
    10. import javax.servlet.http.HttpServletResponse;
    11. import java.io.IOException;
    12. @WebServlet("/user")
    13. public class UserServlet extends HttpServlet {
    14. @Override
    15. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    16. // ApplicationContext app= new ClassPathXmlApplicationContext("applicationContext.xml");
    17. ServletContext servletContext = req.getServletContext();
    18. //ApplicationContext app = (ApplicationContext) servletContext.getAttribute("app");
    19. //变动处
    20. ApplicationContext app = WebApplicationContextUtils.getApplicationContext(servletContext);
    21. UserService userService=app.getBean(UserService.class);
    22. }
    23. }

  • 相关阅读:
    RT-Thread 内存管理(学习一)
    java计算机毕业设计计算机网络精品课程网站源码+mysql数据库+系统+lw文档+部署
    【工程总结】Apollo-Cyber RT 中间件总结1
    样本对应模型例题
    Caffeine缓存的使用
    [基础服务] CentOS7.x 安装Kafka3.x
    半导体器件结构测试稳定性中的加氢效应是什么
    教学资源共享平台的设计
    复原 IP 地址(回溯+剪枝)
    西门子S7-200SMART常见通讯问题解答
  • 原文地址:https://blog.csdn.net/weixin_60719453/article/details/125601107