-1

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!

Monkey
  • 3
  • 1
  • don't add Transactional in the Controllers, Spring its doing that job for you, Spring handle every request, call your services methods, and if some service throws an exception then will rollback the changes, you don't need to say Transactional usually – cralfaro Apr 28 '17 at 12:30
  • Yes,you are right!actually I don`t wanna add Transactional In the Controllers.I wanna add In the Services, as In Services it calls several sql.But when I add transactional in the services as above config ,it doesn`t work.That is my real concern. Thank you for your answer – Monkey Apr 28 '17 at 13:37
  • Then no worries, by default spring handle all methods in your services as transactional – cralfaro Apr 28 '17 at 13:39

2 Answers2

1

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 {

}
e2rabi
  • 4,728
  • 9
  • 42
  • 69
  • finally,I find the error,add use-default-filter="false" in my spring-mvc.context,thank you – Monkey May 09 '17 at 11:56
0

If your service are not implementing interface, you need to set proxy-target-class="true" in tag tx:annotation-driven.

You also should add @Transactional on the service class, or on the function invoked by spring bean directly.

Roc King
  • 431
  • 6
  • 18