SpringMVC_27_需要进行Spring 整合SpringMVC吗?和整合值得注意的地方

->需要进行Spring 整合SpringMVC吗?

->还是否需要再加入Spring的IOC容器?

->是否需要在web.xml文件配置启动Spring IOC容器的 ContextLoaderListener?

1.需要

通常情况下,类似于数据源,事务,整合其他框架都是放在Spring的配置文件中(而不是放在SpringMVC)实际上放入Spring配置文件对应的IOC容器中的还有Service和Dao.

2.不需要

都放在SpringMVC的配置文件中。也可以分多个Spring的配置文件,然后使用import

tips:

1.SpringMVC的IOC容器中的bean可以来引用SpringIOC容器中的bean.

2.反过来呢?则不行。SpringIOC容器中的bean却不能来引用SpringMVC IOC容器中的bean!

问题:

若Spring的IOC容器和SpringMVC的IOC容器扫描的包有重合的部分,就会导致有的bean会被创建2次。

解决:

1.使Spring的IOC容器扫描的包和SpringMVC的IOC容器扫描的包没有重合的部分。

2.使用exclude-filter和include-filter 子节点来规定只能扫描的注解

springmvc.xml​ 只包含include

<context:component-scan base-package="com.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>

spring的配置文件 bean.xml 不包含exclude

<context:componenet-scan base-package="com.springmvc">
	<context:exclude-filter type="annotation"
 		expression="org.springframework.stereotype.Controller"/>
    <context:exclude-filter type="annotation"
      	expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
</context:componenet-scan>

猜你喜欢

转载自blog.csdn.net/weixin_42036647/article/details/86235334