现在的位置: 首页 > 综合 > 正文

springmvc 声明式事物不起作用

2018年05月07日 ⁄ 综合 ⁄ 共 1823字 ⁄ 字号 评论关闭
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- DispatcherServlet Context: defines this servlet's request-processing 
        infrastructure -->

    <!-- Enables the Spring MVC @Controller programming model -->
    <annotation-driven />

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving 
        up static resources in the ${webappRoot}/resources directory -->
    <resources mapping="/resources/**" location="/resources/" />

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources 
        in the /WEB-INF/views directory -->
    <beans:bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <beans:property name="prefix" value="/WEB-INF/pages/" />
        <beans:property name="suffix" value=".jsp" />
    </beans:bean>

    <context:component-scan base-package="jo.geun.controller" />
</beans:beans>

在配置spring-mvc的配置文件时,一定要注意<context:component-scan base-package="jo.geun.controller" /> 

 通常我们会做分层开发,一般分了controller、service 、 dao 三层,由service层来管理事务。

 如果在这个servlet context里面把service层也给scan了,service Bean就会被提前实例化,Spring的事务就没法控制事务对象了。所以一定要让这个扫描器过滤掉service层的扫描。

上面因为package的结构简单,就直接指定了controller层,其实也可以换种方式来写

<context:component-scan base-package="com.geun">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
</context:component-scan>

用上面的方式会过滤掉所有带@Service注解的类,也可以用正则表达式或AspectJ类型的表达式,具体的参阅官方文档。

抱歉!评论已关闭.