整合spring和springmvc----笔记

spring

原先javaweb中使用spring的基本思路是添加过滤器在初始化web应用时创建spring的ioc容器,且添加入context属性中。spring提供了现成的监听器帮我们简化了相应的步骤只需在web.xml中配置

	
<!-- needed for ContextLoaderListener -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>location</param-value>
	</context-param>

	<!-- Bootstraps the root web application context before servlet initialization -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

设置好相应的属性即可。

spirngmvc

springmvc的入口是web.xml配置一个servlet

		<!-- The front controller of this Spring Web application, responsible for handling all application requests -->
	<servlet>
		<servlet-name>springDispatcherServlet</servlet-name>
		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
		<init-param>
			<param-name>contextConfigLocation</param-name>
			<param-value>classpath:springmvc.xml</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>

	<!-- Map all requests to the DispatcherServlet for handling -->
	<servlet-mapping>
		<servlet-name>springDispatcherServlet</servlet-name>
		<url-pattern>/</url-pattern>
	</servlet-mapping>

其实两者的整合与否看业务需求,不整合也可以直接用springmvc的ioc容器即可,本身springmvc就是在spring基础之上。

如要解耦的更彻底:使spirng ioc容器在业务层,而springmvc ioc容器为web容器层,两者可以整合一下

问题1.

由于

<context:component-scan base-package="top.demo.test"></context:component-scan>

扫描的基包在开发中很可能并不会按照功能分开(可能按模块分开),所以两者同时扫描会使一个bean创建两次,此时可以考虑使用子标签分别在sring和springmvc的配置文件中过滤掉重复的部分

<context:component-scan base-package="top.demo.test" use-default-filters="false">
	<context:exclude-filter type="annotation" expression=""/>
	<context:include-filter type="annotation" expression=""/>
</context:component-scan>

问题2.
spirng ioc容器和springmvc容器的关系,两者可以看成父子关系。
spring为父,springmvc为子。

springmvc ioc容器的bean可以引用spring ioc容器的bean
反之则不行

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值