web应用使用spring

web应用使用spring

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

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

java 复制代码
public interface ServletContextListener extends EventListener {
    void contextInitialized(ServletContextEvent var1);

    void contextDestroyed(ServletContextEvent var1);
}

在web.xml中配置listener

xml 复制代码
<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>

zhhll.icu/2021/框架/spr...

本文由mdnice多平台发布

相关推荐
鬼手点金15 小时前
Skills:领域能力的可组合单元
java·人工智能·笔记·自动化·1024程序员节
captain3761 天前
MyBatis(入门5)其他查询操作
java·数据库·spring boot·mybatis
Novproxy_Z1 天前
商用代理避坑:如何识别商家拿机房 IP 冒充住宅 IP
java·网络·tcp/ip
文丑颜不良啊1 天前
LeetCode1481~1500题目解析
java·leetcode
2601_962885721 天前
quotes 字段全解:涨跌幅、振幅、换手率的口径与单位
java·前端·spring
前端精髓1 天前
调用openrouter的免费API模型
java·前端·javascript
步行cgn1 天前
Spring MVC 拦截器 Interceptor 详解:从执行流程到实战应用
java·spring·mvc
文丑颜不良啊1 天前
LeetCode1381~1400题目解析
java·leetcode
开开心心_Every1 天前
快速隐藏程序工具自定义快捷键一键切换
java·开发语言·前端·spring·智能手机·word·intellij-idea
萧瑟余晖1 天前
Spring Batch核心模型与快速上手详解
java·spring·batch