Search code examples
spring-mvcmybatistransactional

Spring @Transactional works on Controller but Service has no effect


This is my 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>

This is my 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>

This is my 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"/>

This is my app-context.xml

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

This is my 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>

when I add @Transactional to Controller ,transaction it works but Service it does`t work! the following pic is @Transactional on Service enter image description here the following pic is @Transactional on Controller enter image description here

I really can`t finger it out,Thanks for help!


Solution

  • I think You have to enable transaction using configuration add this annotation @EnableTransactionManagement in a config class

    Create a class for configuration :

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