springmvc3.0+mybatis 加入@Transactional后导致整个类都无法访问是怎么回事,事务没有启动吗?跪求大神帮忙解答

时间:2021-07-14 20:35:55
         下面是我的代码分为applicationContext.xml 、srping-mvc.xml 、LoginController 、serviceImpl实现类,在serviceImple中加入 @Transcation后LoginController便无法访问这个service实现类的方法,没出异常,如果去掉@Transcation又可以访问这个serviceImpl了,我在想是哪个地方和@Transcation冲突了,求大神解决
这个问题困扰了我很多天了,跪求大神给出意见,在网上找了很多相关资料,没有得到解决



#######################################applicationContext#####################################
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd"
       default-lazy-init="true">

<description>Spring公共配置 applicationContext.xml</description>

  <context:property-placeholder order="1" ignore-unresolvable="true" location="/WEB-INF/jdbc.properties" />
        <!-- 数据源配置, 使用DBCP数据库连接池 -->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
            <!-- Connection Info -->
            <property name="driverClassName" value="${jdbc.driver}"/>
            <property name="url" value="${jdbc.url}" />
            <property name="username" value="${jdbc.username}" />
            <property name="password" value="${jdbc.password}" />
            <property name="maxActive" value="${jdbc.pool.maxActive}" />
            <property name="maxIdle" value="${jdbc.pool.maxIdle}" />
            <property name="defaultAutoCommit" value="false" />
            <!-- 连接Idle一个小时后超时 -->
            <property name="timeBetweenEvictionRunsMillis" value="3600000" />
            <property name="minEvictableIdleTimeMillis" value="3600000" />
        </bean>
        
    <!-- 使用annotation 自动注册bean, 并保证@Required、@Autowired的属性被注入 -->
    <context:component-scan base-package="com.srx">
     <!-- 避免重复扫描  controller交给web.xml去扫描-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <context:exclude-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice"/>
    </context:component-scan>
        
<!-- MyBatis配置 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
        <property name="typeAliasesPackage" value="com.srx" />
       <property name="mapperLocations" value="classpath:com/srx/**/*Mapper.xml" />
        <property name="configLocation" value="/WEB-INF/mybatis-config.xml"></property>
        
        <property name="plugins">
            <array>
                <bean class="com.github.miemiedev.mybatis.paginator.OffsetLimitInterceptor">
                    <property name="dialectClass" value="com.github.miemiedev.mybatis.paginator.dialect.OracleDialect"/>
                    <property name="asyncTotalCount" value="false"/>
                </bean>
            </array>
        </property>
    </bean>
    
<!-- 扫描basePackage下所有以@MyBatisRepository标识的 接口 -->
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.srx" />
<!--自己写了个接口注解用于dao-->
        <property name="annotationClass" value="com.srx.core.utils.myBatis.MyBatisRepo"/>
    </bean>
    
    <context:annotation-config />
    <!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>

<!-- 事务管理器配置, 使用jdbc事务 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>


##################################spring-mvc.xml#######################################


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd">
 
    <!-- 过滤Service注入,否则事务无效 -->
<context:component-scan base-package="com.srx">
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>
    
    <mvc:annotation-driven>
        <mvc:message-converters register-defaults="true">
            <!-- 将StringHttpMessageConverter的默认编码设为UTF-8 -->
            <bean class="org.springframework.http.converter.StringHttpMessageConverter">
                <constructor-arg value="UTF-8" />
            </bean>
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
                <property name="objectMapper">
                    <bean class="com.github.miemiedev.mybatis.paginator.jackson2.PageListJsonMapper" />
                </property>
            </bean>
        </mvc:message-converters>
    </mvc:annotation-driven>

    <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
    <!-- 定义JSP文件的位置 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>

    <!-- 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL-->
    <mvc:default-servlet-handler/>

    <!-- 定义无需Controller的url<->view直接映射 -->
    <mvc:view-controller path="/" view-name="redirect:/myWorkBench/default"/>

    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/**"/>
            <mvc:exclude-mapping path="/*"/>
            <mvc:exclude-mapping path="/core/layout/decorator/*"/>
            <bean class="com.srx.core.layouts.interceptors.LayoutInterceptor"></bean>
        </mvc:interceptor>
        <mvc:interceptor>
            <mvc:mapping path="/**" />
            <bean class="com.github.miemiedev.mybatis.paginator.springmvc.PageListAttrHandlerInterceptor" />
        </mvc:interceptor>
    </mvc:interceptors>
</beans>


####################################java控制器######################################
@Controller
public class LoginController {

@Autowired
protected UserService userService;

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login() {
userService.findUserByLoginName("aa");
return "login";
}
}
####################################service实现类####################################
@Service
@Transactional(rollbackFor = Exception.class)
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Override
    public User findUserByLoginName(String loginName) {
     System.out.println(userDao);// 不加Transactional的话就可以打打印出来,加了就进入不了这个类一样
        return userDao.findByLoginName(loginName);
    }
}

3 个解决方案

#1


注解加方法上边试试?

#2


@Service
@Transactional(rollbackFor = Exception.class)
改成:
@Service("UserService ")
@Transactional

#3


引用 2 楼 qq_15629213 的回复:
@Service
@Transactional(rollbackFor = Exception.class)
改成:
@Service("UserService ")
@Transactional


谢谢了啊,不是这个的问题,是因为数据库初始化的时候并没有主动链接,我在dataSource后面加入了 init-method="getConnection" 后就有用了

#1


注解加方法上边试试?

#2


@Service
@Transactional(rollbackFor = Exception.class)
改成:
@Service("UserService ")
@Transactional

#3


引用 2 楼 qq_15629213 的回复:
@Service
@Transactional(rollbackFor = Exception.class)
改成:
@Service("UserService ")
@Transactional


谢谢了啊,不是这个的问题,是因为数据库初始化的时候并没有主动链接,我在dataSource后面加入了 init-method="getConnection" 后就有用了