开启事务注解驱动报错问题 或 ‘xx‘ defined in class path resource [application.xml]: Initialization of bean failed

本文档记录了一位开发者在学习Spring声明式事务控制时,因版本升级导致的初始化失败问题。通过调整spring-tx依赖至5.0.3.RELEASE解决了冲突,涉及到了@Autowired和@TransactionManagement的注解依赖问题。
摘要由CSDN通过智能技术生成

项目场景:

小白学习spring基于注解的声明式事务控制时,遇到的问题


问题描述:

提示:这里描述项目中遇到的问题:
例如:数据传输过程中数据不时出现丢失的情况,偶尔会丢失一部分数据
APP 中接收数据代码:

按学习步骤,在配置文件上开启事务的注解驱动

<tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>

 运行时报错

����: Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AccountDaoImpl': Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in class path resource [application.xml]: Cannot resolve reference to bean 'comboPooledDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'comboPooledDataSource' defined in class path resource [application.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;)Lorg/springframework/core/annotation/AnnotationAttributes;
java.lang.ExceptionInInitializerError
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'AccountDaoImpl': Unsatisfied dependency expressed through field 'template'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jdbcTemplate' defined in class path resource [application.xml]: Cannot resolve reference to bean 'comboPooledDataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'comboPooledDataSource' defined in class path resource [application.xml]: Initialization of bean failed; nested exception is java.lang.NoSuchMethodError: org.springframework.core.annotation.AnnotatedElementUtils.getAnnotationAttributes(Ljava/lang/reflect/AnnotatedElement;Ljava/lang/String;)Lorg/springframework/core/annotation/AnnotationAttributes;
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:587)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:373)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1344)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:578)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:501)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:317)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:315)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:199)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:760)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:869)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:550)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
	at com.itheima.controller.AccountController.<clinit>(AccountController.java:7)

 起初一直认为在创建名为comboPooledDataSource的bean时出错,该bean定义在类路径resource [application.xml]:bean初始化失败;

百度很多文章都没能解决

查看了命名空间是否有写错误等等

 BeanCreationException: Error creating bean with name 'comboPooledDataSource' defined in class path resource [application.xml]: Initialization of bean failed;

原因分析:

我在做基于xml的声明式事务控制时,没有comboPooledDataSource初始化失败的问题,所以我把注解的事务驱动注释掉,能正常运行。开启就会报错。

在一头雾水后,我破罐子破摔,明白遇事不决,先改相关jar包的版本


解决方案:

这是报错时用的版本信息

<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-tx</artifactId>
    <version>4.1.7.RELEASE</version>
</dependency>

我提高版本信息后就能正常运行

<version>5.0.3.RELEASE</version>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值