SpringMVC:@Transactional 原因:没有定义 [...] 类型的限定 bean
SpringMVC: @Transactional causes: No qualifying bean of type [...] is defined
我的主要功能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);
}
}
我的 BaseFck class:
@Service
public interface BaseFck
{
@Transactional
void test();
}
我的 BaseFckImpl class:
@Service
public class BaseFckImpl implements BaseFck
{
@Transactional
public void test()
{
Log.debug("------test---------");
}
}
我的一部分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,那么它运行得很好。
为什么 @Transactional 导致此错误
有什么想法吗?
你需要
<tx:annotation-driven proxy-target-class="true"/>
让 Spring 使用 CGLIB class 代理而不是默认的 JDK 接口代理。
我不知道为什么你的配置中有两个 <component-scan>
元素,但你只需要一个。
它应该有一个 base-package
值代表开始查找的基础包,即。包含您的 @Service
类型的那个。 component-scan
采用层次结构。给定一个像
这样的包结构
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.*" />
不支持。
我的主要功能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);
}
}
我的 BaseFck class:
@Service
public interface BaseFck
{
@Transactional
void test();
}
我的 BaseFckImpl class:
@Service
public class BaseFckImpl implements BaseFck
{
@Transactional
public void test()
{
Log.debug("------test---------");
}
}
我的一部分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,那么它运行得很好。
为什么 @Transactional 导致此错误
有什么想法吗?
你需要
<tx:annotation-driven proxy-target-class="true"/>
让 Spring 使用 CGLIB class 代理而不是默认的 JDK 接口代理。
我不知道为什么你的配置中有两个 <component-scan>
元素,但你只需要一个。
它应该有一个 base-package
值代表开始查找的基础包,即。包含您的 @Service
类型的那个。 component-scan
采用层次结构。给定一个像
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.*" />
不支持。