至少需要7个jar

image-20210123154134146

新建一个动态web项目

配置web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
id="WebApp_ID" version="4.0">
<display-name>SpringWeb1</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<!-- 指定IOC容器的位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!-- 配置Spring-web.jar提供的监听器,此监听器可以在服务器启动时,初始化IOC容器 初始化IOC容器,必须告诉此容器的位置:contextparam -->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

新建一个applicationContext

启动tomcat

信息: Loading XML bean definitions from class path resource [applicationContext.xml]

加载多个XML

1
2
3
4
5
6
7
8
9
<context-param>
<!-- 指定IOC容器的位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml,
classpath:applicationContext-*.xml
</param-value>
<!-- 加载多个applicationContext -->
</context-param>

或者

在主配置文件中

<import resource="applicationContext-Controller.xml"/>

1
2
3
4
5
6
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">
<import resource="applicationContext-Controller.xml"/>
</beans>