【知识整理】Spring整合SpringMVC

一.Spring整合SpringMVC:通常情况下,类似于数据源、整合其他框架、事务都放在Spring的配置文件中,放入Spring中的配置文件对应的IOC中的还有Service和DAO。而与Handler相关的配置都放在SpringMVC配置文件中。

1.Spring整合SpringMVC流程:

(1)web.xml中:

<!-- 配置启动Spring IOC容器的Listener -->
<context-param>
	<param-name>contextConfigLocation</param-name>
	<param-value>classpath:beans.xml</param-value>
</context-param>
<listener>
	<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- 配置DispatcherServlet -->
<servlet>
	<servlet-name>springDispatcherServlet</servlet-name>
	<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
	<!-- 配置DespatcherServlet的一个初始化参数:配置springMVC配置文件的位置和名称-->
	<init-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:springmvc.xml</param-value>
	</init-param>
	<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
	<servlet-name>springDispatcherServlet</servlet-name>
	<url-pattern>/</url-pattern>
</servlet-mapping>
(2)SprimgMVC配置文件中进行Handler相关配置:

注:为防止扫描的文件发生冲突,导致了两次对象应在扫描的文件种进行如下修改:(表示仅自动扫描Controller类及ControllerAdvice 类)

<context:component-scan base-package="com.lyn.springmvc" use-default-filters="false">
	<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>
(3)Spring配置文件中进行数据源、整合其他框架、事务、Service、DAO的相关配置
注:为防止扫描的文件发生冲突,导致了两次对象应在扫描的文件种进行如下修改:(表示仅不扫描Controller类及ControllerAdvice类)

<!-- 配置自动扫描的包 -->
<context:component-scan base-package="com.lyn.springmvc" use-default-filters="false">
	<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
	<context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:component-scan>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值