关于Spring中整合Web
第一步:注册ContextLoaderListener
在web.xml文件中需要配置ContextLoaderListener,这样Web中可以加载Spring中的ApplicationContext,会将这个对象保存在Web应用程序的ServletContext中。
Sping的配置文件默认在/WEB-INF/下面,如果你想把spring的配置文件放在src中,,
在<param-value>classpath:applicationContext.xml</param-value>
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/9602a725015dd776f2e0188ca4a1d48d.png)
第二步:使用WebApplicationContext
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/6ba4ab7e75ee96a67e0588c988086064.png)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>spring-5</display-name>
<!-- needed for ContextLoaderListener -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:application.xml</param-value>
</context-param>
<!-- Bootstraps the root web application context before servlet initialization -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
Talking is cheap ,Code is expensive!