本文介绍了SpringMVC:@Transactional原因:没有定义类型为[...]的合格bean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的主函数类:

public class Database2Redis
{
    public static void test(ApplicationContext applicationContext)
    {
        BaseFckImpl service = applicationContext.getBean(BaseFckImpl.class);
        // ...
    }

    public static void main(String[] args) throws Exception
    {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring-config.xml");

        test(applicationContext);
    }
}

我的BaseFck类:

My BaseFck class:

@Service
public interface BaseFck
{
    @Transactional
    void test();
}

我的BaseFckImpl类:

My BaseFckImpl class:

@Service
public class BaseFckImpl implements BaseFck
{
    @Transactional
    public  void test()
    {
            Log.debug("------test---------");
    }
}

spring-config.xml的一部分

part of my spring-config.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:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop"
    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/tx
    http://www.springframework.org/schema/tx/spring-tx-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/aop
    http://www.springframework.org/schema/aop/spring-aop-3.2.xsd">


    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>classpath:jdbc.properties</value>
                <value>classpath:config.properties</value>
            </list>
        </property>
    </bean>
    <util:properties id="systemConfigProperties" location="classpath:config.properties"/>


    <context:component-scan base-package="com.*" />
    <bean name="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    <tx:annotation-driven transaction-manager="transactionManager" />
    <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.**.mapper" />
        <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
    </bean>


    <mvc:annotation-driven />
    <mvc:default-servlet-handler />
    <context:component-scan base-package="xxxx" />
    <bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
    <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
</beans>

错误消息:

Exception in thread "main" org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.service.impl.BaseFckImpl] is defined
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:296)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1127)
    at com.database2redis.Database2Redis.businessAccount2redis(Database2Redis.java:18)
    at com.Database2Redis.main(Database2Redis.java:29)

如果我删除@Transactional,那么它运行得很好.

if I remove @Transactional, then it runs very well.

为什么@Transactional导致此错误

why @Transactional is causing this error

有什么想法吗?

推荐答案

您将需要

<tx:annotation-driven proxy-target-class="true"/>

让Spring使用CGLIB类代理而不是默认的JDK接口代理.

to have Spring use CGLIB class proxies rather than the default JDK interface proxies.

我不知道为什么您的配置中有两个<component-scan>元素,但是您只需要一个.

I don't know why you have two <component-scan> elements in your configuration, but you only need one.

它应该有一个base-package值,代表开始寻找的基本包装,即.一个包含您的@Service类型的类型. component-scan假定为层次结构.给定类似

It should have a base-package value representing the base package to start looking, ie. the one containing your @Service types. component-scan assumes a hierarchical structure. Given a package structure like

com
com/example
com/example/deep
com/example/another
com/final

扫描

<context:component-scan base-package="com.example" />

将扫描所有

com/example
com/example/deep
com/example/another

您要与*

<context:component-scan base-package="com.*" />

不受支持.

这篇关于SpringMVC:@Transactional原因:没有定义类型为[...]的合格bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-22 09:28