基于Spring开发web项目

一、导入相关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>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值