Spring @Transactional适用于Controller,但Service不起作用

时间:2017-04-28 10:31:21

标签: spring-mvc mybatis transactional

这是我的web.xml

  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/app-root.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>springmvc</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>springmvc</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

这是我的springmvc-servlet.xml

 <mvc:annotation-driven/>
    <mvc:default-servlet-handler/>

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

这是我的app-root.xml

   <import resource="classpath:spring/app-dao.xml"/>
    <import resource="classpath:spring/shiro.xml"/>
    <import resource="classpath:spring/app-timer.xml"/>
    <import resource="classpath:spring/app-context.xml"/>

这是我的app-context.xml

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

这是我的app-datasource.xml

<bean id="adminTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>

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

    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
        <property name="configLocation" value="classpath:spring/mybatis-config.xml"/>
        <!--扫描entity包,使用别名-->
        <property name="typeAliasesPackage" value="com.xinshen.meeting.model"/>
        <property name="mapperLocations" value="classpath*:mappers/*.xml"/>

        <property name="plugins">
            <array>
                <bean class="com.github.pagehelper.PageHelper">
                    <property name="properties">
                        <value>
                            dialect=mysql
                        </value>
                    </property>
                </bean>
            </array>
        </property>

    </bean>

    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.***.***.dao"/>
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
    </bean>

当我将@Transactional添加到Controller时,它可以正常运行但服务它确实有效! 以下图片是@Transactional on Service enter image description here 以下图片是控制器上的@Transactional enter image description here

我真的不能指责它,谢谢你的帮助!

2 个答案:

答案 0 :(得分:1)

我认为您必须使用配置启用事务在配置类中添加此注释 @EnableTransactionManagement

创建配置类:

@Configuration
@ComponentScan(basePackages = { "Your services package or base package" })
@EnableTransactionManagement
public class MyConfig {

}

答案 1 :(得分:0)

如果您的服务未实现接口,则需要在标签tx:annotation-driven中设置proxy-target-class =“ true”。

您还应该在服务类上或在直接由spring bean调用的函数上添加@Transactional。