Spring @Transactional 适用于 Controller 但 Service 无效

Spring @Transactional works on Controller but Service has no effect

这是我的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>

这是我的应用-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"/>

这是我的应用-context.xml

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

这是我的应用-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

实在是摸不着头脑,谢谢帮忙!

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

为配置创建 class:

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

}

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

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