一、导入相关jar包
web项目的jar包是存入到WEB-INF/lib中
二、初始化IOC容器
初始化IOC容器可以通过配置监听器实现。
思路:当服务启动时(tomcat),通过监听器将SpringIOC容器初始化(该监听器 spring-web.jar已经提供)。web项目启动时,会自动加载web.xml,因此需要在web.xml中加载监听器。
<!-- 指定 Ioc容器(applicationContext.xml)的位置-->
<context-param>
<!-- 监听器的父类ContextLoader中有一个属性contextConfigLocation,该属性值保存着容器配置文件applicationContext.xml的位置 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<!-- 配置spring-web.jar提供的监听器,此监听器 可以在服务器启动时 初始化Ioc容器。
初始化Ioc容器(applicationContext.xml),
1.告诉监听器 此容器的位置:context-param
2.默认约定的位置 :WEB-INF/applicationContext.xml
-->
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
三、配置Tomcat
百度有很多,这里不再阐述。
https://jingyan.baidu.com/article/09ea3ede48b425c0aede39d4.html
四、拆分Spring配置文件
1.根据3层结构拆分
可分为dao层,service层,controller层,公共层。
2.根据功能结构拆分
例如:学生相关配置 applicationContextStudent.xml
班级相关配置 applicationContextClass.xml
加载多个配置文件
(一)在web.xml文件中加载
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:applicationContext.xml,
classpath:applicationContext-*.xml
</param-value>
</context-param>
(二)在主配置文件中引入其他文件
<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-*.xml"/>
</beans>