• (续)SSM整合之SSM整合笔记(ContextLoaderListener)(P177-178)


    目录

    ContextLoaderListener

    一  ContextLoaderListener

    二   测试ContextLoaderListener

    1 新建模块spring_listener     com.atguigu

    2. 导入依赖

    3 .转web

    4 .web.xml

    5 springmvc.xml

    6 .spring.xml

    7  首页index.html

    8 控制层 HelloController

    9  service接口HelloService 

    10 service实现类

    11   配置到tomcat中并启动

    12 测试结果


    ContextLoaderListener

    一  ContextLoaderListener

    Spring 提供了监听器 ContextLoaderListener ,实现 ServletContextListener 接口,可监听
    ServletContext 的状态,在 web 服务器的启动,读取 Spring 的配置文件,创建 Spring IOC 容器。 web应用中必须在 web.xml 中配置

     

    1. <listener>
    2. <listenerclass>org.springframework.web.context.ContextLoaderListenerlistener-class>
    3. listener>
    4. <context-param>
    5. <param-name>contextConfigLocationparam-name>
    6. <param-value>classpath:spring.xmlparam-value>
    7. context-param>

    二   测试ContextLoaderListener

    1 新建模块spring_listener     com.atguigu

    2. 导入依赖

    war
    
    
        
        
            org.springframework
            spring-webmvc
            5.3.1
        
    
        
        
            ch.qos.logback
            logback-classic
            1.2.3
        
    
        
        
            javax.servlet
            javax.servlet-api
            3.1.0
            provided
        
    
        
        
            org.thymeleaf
            thymeleaf-spring5
            3.0.12.RELEASE
        
    
    

    3 .转web

    4 .web.xml

    1. <servlet>
    2. <servlet-name>SpringMVCservlet-name>
    3. <servlet-class>org.springframework.web.servlet.DispatcherServletservlet-class>
    4. <init-param>
    5. <param-name>contextConfigLocationparam-name>
    6. <param-value>classpath:springmvc.xmlparam-value>
    7. init-param>
    8. <load-on-startup>1load-on-startup>
    9. servlet>
    10. <servlet-mapping>
    11. <servlet-name>SpringMVCservlet-name>
    12. <url-pattern>/url-pattern>
    13. servlet-mapping>
    14. <listener>
    15. <listener-class>org.springframework.web.context.ContextLoaderListenerlistener-class>
    16. listener>
    17. <context-param>
    18. <param-name>contextConfigLocationparam-name>
    19. <param-value>classpath:spring.xmlparam-value>
    20. context-param>

    5 springmvc.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xmlns:mvc="http://www.springframework.org/schema/mvc"
    6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    7. <context:component-scan base-package="com.atguigu.controller">context:component-scan>
    8. <bean id="viewResolver" class="org.thymeleaf.spring5.view.ThymeleafViewResolver">
    9. <property name="order" value="1"/>
    10. <property name="characterEncoding" value="UTF-8"/>
    11. <property name="templateEngine">
    12. <bean class="org.thymeleaf.spring5.SpringTemplateEngine">
    13. <property name="templateResolver">
    14. <bean class="org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver">
    15. <property name="prefix" value="/WEB-INF/templates/"/>
    16. <property name="suffix" value=".html"/>
    17. <property name="templateMode" value="HTML5"/>
    18. <property name="characterEncoding" value="UTF-8" />
    19. bean>
    20. property>
    21. bean>
    22. property>
    23. bean>
    24. <mvc:annotation-driven />
    25. <mvc:view-controller path="/" view-name="index">mvc:view-controller>
    26. beans>

    6 .spring.xml

    1. "1.0" encoding="UTF-8"?>
    2. <beans xmlns="http://www.springframework.org/schema/beans"
    3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    4. xmlns:context="http://www.springframework.org/schema/context"
    5. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">
    6. <context:component-scan base-package="com.atguigu.service.impl">context:component-scan>
    7. beans>

    7  首页index.html

    1. html>
    2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>首页title>
    6. head>
    7. <body>
    8. <h1>index.htmlh1>
    9. body>
    10. html>

    8 控制层 HelloController

    1. package com.atguigu.controller;
    2. @Controller
    3. public class HelloController {
    4. @Autowired
    5. private HelloService helloService;
    6. }

    9  service接口HelloService 

    1. package com.atguigu.service;
    2. public interface HelloService {
    3. }

    10 service实现类

    1. package com.atguigu.service.impl;
    2. @Service
    3. public class HelloServiceImpl implements HelloService {
    4. }

    11   配置到tomcat中并启动

    12 测试结果

     

  • 相关阅读:
    贪心算法之乘船问题
    SpringBoot的Condition注解
    使用 Django 构建简单 Web 应用
    PostgreSQL数据库动态共享内存管理器——dynamic shared memory segment
    父子项目打包发布至私仓库
    实现定时器的两种方法:使用windows api定时器 和使用c++11/14 定时器
    农业温室大棚智能远程监控系统,完美实现无人值守
    MST2101Q2 摩托车三相磁电机调压器控制芯片
    【汇编】#5 80x86指令系统其一(数据传送与算术)
    常见数据库优化面试题
  • 原文地址:https://blog.csdn.net/m0_59281987/article/details/127992968