SpringMVC: @Transactional causes: No qualifying be

2019-08-29 23:27发布

My main function class:

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);
    }
}

My BaseFck class:

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

My BaseFckImpl class:

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

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>

error message:

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)

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

why @Transactional is causing this error

Any ideas?

1条回答
对你真心纯属浪费
2楼-- · 2019-08-30 00:09

You'll need

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

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

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

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

scanning with

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

will scan all of

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

The notation you're trying to use with the *

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

is not supported.

查看更多
登录 后发表回答