主题:【问题、Spring、事务】关于 @Transactional 无法注入的问题

liumangcjd 2017年01月11日 90

去掉 @Transactional 事务注解就可以正常启动,求帮助 ...


版本信息(使用官方文档推荐版本):

    <dependency>
        <groupId>com.ibeetl</groupId>
        <artifactId>beetl</artifactId>
        <version>2.7.3</version>
    </dependency>
        
    <dependency>
        <groupId>com.ibeetl</groupId>
        <artifactId>beetlsql</artifactId>
        <version>2.7.7</version>
    </dependency>


XML配置部分:

    <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 使用annotation注解方式配置事务 -->
    <tx:annotation-driven transaction-manager="txManager" />


Java 代码部分:

    @Transactional()
    public String tranTest(){
        PortEmp emp = new PortEmp();
        
        emp.setNo("tranTest");
        emp.setName("应该回滚");
        
        this.dao.insert(emp);
        
        Integer temp = 1;
        
        if (temp == 1) {
            throw new RuntimeException("测试回滚错误");
        }
        
        return "添加成功";
    }


启动报错(无法注入):

2017-01-11 16:26:52
[ERROR]-[Thread: localhost-startStop-1]-[org.springframework.web.context.ContextLoader.initWebApplicationContext()]: Context initialization failed
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'indexController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gxlc.app.service.IndexService com.gxlc.app.web.IndexController.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.gxlc.app.service.IndexService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1208)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:476)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:303)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:299)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:194)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:755)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:762)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:480)
    at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:434)
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:306)
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:106)
    at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:5118)
    at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5634)
    at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:145)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
    at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.gxlc.app.service.IndexService com.gxlc.app.web.IndexController.service; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.gxlc.app.service.IndexService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:561)
    at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:88)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:331)
    ... 22 more
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.gxlc.app.service.IndexService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:1301)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1047)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:942)
    at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:533)
    ... 24 more
闲大赋 2017年01月11日

还没有没有别的报错信息,单看这个似乎看不出来跟beetl相关的错误,


beetl实现食物是通过TransactionSynchronizationManager 来判断当前是否处于事物状态来决定是否要提交事物,因此不太可能影响@Transational配置,


你以前配置过spring这块吗,你是新手还是老手,如果你要参考例子,可以看看这个demo


http://git.oschina.net/xiandafu/springbeetlsql/blob/master/src/main/java/com/park/oss/service/impl/UserServiceImpl.java?dir=0&filepath=src%2Fmain%2Fjava%2Fcom%2Fpark%2Foss%2Fservice%2Fimpl%2FUserServiceImpl.java&oid=de55dce85db50c55dc3a1e73f84386a28bbe3063&sha=ad5902137b217f4c1ceea62c4cd4dc9b461e2c0a


以及相应的spring配置


http://git.oschina.net/xiandafu/springbeetlsql/tree/master/src/main/webapp/WEB-INF?dir=1&filepath=src%2Fmain%2Fwebapp%2FWEB-INF&oid=35b28fde533ec66e59ebb8e0034a7e8642fb1a55&sha=ad5902137b217f4c1ceea62c4cd4dc9b461e2c0a

liumangcjd 9天前

问题已解决,

因为JDK默认的Proxy代理类库只支持 接口(Interface) + 实现类(Impl)方式注入,把 service 的结构改成这样就可以了,

至于为什么加了事务注解才这样 和 如何直接类注入,正在研究中,有解了再来补充...

zhangyq 8天前

没太明白楼上说的意思? @liumangcjd