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多平台发布

相关推荐
Diligently_几秒前
Anolis 系统更新与密码设置&磁盘扩容
java·后端·spring
mqiqe13 分钟前
线程调度与 Schedulers:Project Reactor 并发模型的核心引擎
java·开发语言·网络
我命由我1234524 分钟前
Android 开发问题:unexpected element <service> found in <manifest>
android·java·java-ee·android studio·android jetpack·android-studio·android runtime
正在走向自律1 小时前
飞算JavaAI能搞定充电站故障处置吗?
java·ai编程·java开发·飞算javaai·java代码生成·ai coding模型·springboo
用户40966601317511 小时前
Guava 不止有 Lists 和 Maps:Cache、EventBus、Multimap 实战
java·后端
一只叫煤球的猫1 小时前
从 Java 21 到 Java 25:ThreadLocal 以外的选择—— ScopedValue
java·后端·架构
步行cgn2 小时前
MyBatis 多对一关联映射详解
java
仍然.2 小时前
SpringCloud---Eureka介绍
java·spring cloud·eureka
霸道流氓气质2 小时前
Spring 事务同步机制 —— AfterTransactionActionCollector 的异步解耦原理
java·后端·spring
Lightpwd2 小时前
PostgreSQL jsonb 写入踩坑:一条 `\u0000` 如何让整批同步失败,还把异常伪装成“数据完整性冲突”
java