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

相关推荐
木木子99993 小时前
业务架构、应用架构、数据架构、技术架构
java·开发语言·架构
qq_5470261795 小时前
Flowable 工作流引擎
java·服务器·前端
鼓掌MVP6 小时前
Java框架的发展历程体现了软件工程思想的持续进化
java·spring·架构
编程爱好者熊浪6 小时前
两次连接池泄露的BUG
java·数据库
lllsure6 小时前
【Spring Cloud】Spring Cloud Config
java·spring·spring cloud
鬼火儿7 小时前
SpringBoot】Spring Boot 项目的打包配置
java·后端
NON-JUDGMENTAL7 小时前
Tomcat 新手避坑指南:环境配置 + 启动问题 + 乱码解决全流程
java·tomcat
chxii7 小时前
Maven 详解(上)
java·maven
李少兄7 小时前
IntelliJ IDEA 远程调试(Remote Debugging)教程
java·ide·intellij-idea
Kuo-Teng8 小时前
Leetcode438. 找到字符串中所有字母异位词
java·算法·leetcode