web.xml中定义的Spring的XML配置文件启动顺序

在web.xml中定义的Spring的配置文件一般有两个:
1、Spring上下文环境的配置文件:applicationContext.xml

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            classpath:applicationContext.xml
        </param-value>
    </context-param>

2、SpringMVC配置文件:spring-servlet.xml

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-servlet.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

加载顺序是:首先加载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" />

Men were born to be suffering, the pain of struggle, or the pain of regret?

  • 8
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值