Spring和SpringMVC父子容器关系初窥

https://www.cnblogs.com/hafiz/p/5875740.html

这个写的很好,是我想要的答案
http://blog.csdn.net/trigl/article/details/52073457

加载顺序是:首先加载Spring上下文环境配置文件,然后加载SpringMVC配置文件,并且如果配置了相同的内容,SpringMVC配置文件会被优先使用。
所以这里需要注意一个问题,一定要注意SpringMVC配置文件内容不要把Spring上下文环境配置文件内容覆盖掉。
比如在Spring上下文环境配置文件中先引入service层,然后又加入了事务:

<context:component-scan base-package="com.acms.service"></context:component-scan>
    <!-- define the transaction manager -->
    <bean id="transactionManagerOracle"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceOracle" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManagerOracle" />

但是在SpringMVC配置文件中却默认引入所有类(当然也包括service层),但是没有加入事务

<context:component-scan base-package="com.acms"></context:component-scan>

那么这时事务功能是无法起作用的,也就是代码中加入@Transactional注解是无效的。

所以为了防止这种问题一般是在Spring上下文配置文件中引入所有的类,并且加上事务:

<context:component-scan base-package="com.acms"></context:component-scan>
    <!-- define the transaction manager -->
    <bean id="transactionManagerOracle"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSourceOracle" />
    </bean>

    <tx:annotation-driven transaction-manager="transactionManagerOracle" />

而在SpringMVC配置文件中只引入controller层:

 <context:component-scan base-package="com.acms.controller" />
    <context:component-scan base-package="com.acms.*.controller" />
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值