web应用使用spring

web应用使用spring

对于java而言,使用最多的还是web开发,如何在web应用中使用spring呢,web应用中没有main方法,而且必须在servlet容器加载时就创建spring的IOC容器,之前在学习Servlet的时候有讲到一个Servlet监听器,可以监听ServletContext、HttpSession、ServletRequest等对象的创建和销毁

所以可以实现ServletContextListener接口,重写contextInitialized方法来实现spring的IOC容器的创建

public interface ServletContextListener extends EventListener {
    void contextInitialized(ServletContextEvent var1);

    void contextDestroyed(ServletContextEvent var1);
}

在web.xml中配置listener

<listener>
 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 指明配置文件所在位置 -->
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value>
</context-param>

https://zhhll.icu/2021/框架/spring/基础/10.web应用使用spring/

本文由 mdnice 多平台发布

相关推荐

  1. web应用使用spring

    2024-04-21 03:58:02       16 阅读
  2. 使用Spring Boot快速构建Spring应用

    2024-04-21 03:58:02       11 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-04-21 03:58:02       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-04-21 03:58:02       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-04-21 03:58:02       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-04-21 03:58:02       18 阅读

热门阅读

  1. 2024.4.20力扣每日一题——组合总和

    2024-04-21 03:58:02       11 阅读
  2. 游戏中的伤害类型

    2024-04-21 03:58:02       11 阅读
  3. 正则表达式大全,30个正则表达式详细案例

    2024-04-21 03:58:02       17 阅读
  4. 上海计算机学会2023年12月月赛C++丙组T2移动复位

    2024-04-21 03:58:02       13 阅读
  5. 搭建vue3组件库(一):Monorepo项目搭建

    2024-04-21 03:58:02       16 阅读
  6. Docker常见命令学习

    2024-04-21 03:58:02       17 阅读
  7. mac修改/etc/profile导致终端所有命令不可使用

    2024-04-21 03:58:02       15 阅读
  8. CentOS系统上经常使用的一些基本命令

    2024-04-21 03:58:02       13 阅读
  9. android11启动服务

    2024-04-21 03:58:02       13 阅读