首页 新闻 会员 周边 捐助

Spring声明式事务报错: org.springframework.beans.factory.parsing.BeanDefinitionParsingException

0
悬赏园豆:10 [已关闭问题] 关闭于 2018-11-20 13:12

最近在学习Spring事务管理,在编写声明式事务管理时,总是报错,检查transaction.xml里面配置的内容也没找到问题,后来改为注解式事务管理,还是报同样的问题,相关的jar包(包括aopalliance-1.0.jar)也已经加进来了,不知道什么原因,求各位大神指导指导。

错误信息:
Exception in thread "main" org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Cannot locate BeanDefinitionDecorator for element [advice]
Offending resource: class path resource [transaction.xml]
at org.springframework.beans.factory.parsing.FailFastProblemReporter.fatal(FailFastProblemReporter.java:62)
at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:90)
at org.springframework.beans.factory.parsing.ReaderContext.fatal(ReaderContext.java:68)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.findDecoratorForNode(NamespaceHandlerSupport.java:125)
at org.springframework.beans.factory.xml.NamespaceHandlerSupport.decorate(NamespaceHandlerSupport.java:101)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateIfRequired(BeanDefinitionParserDelegate.java:1404)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1390)
at org.springframework.beans.factory.xml.BeanDefinitionParserDelegate.decorateBeanDefinitionIfRequired(BeanDefinitionParserDelegate.java:1370)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.processBeanDefinition(DefaultBeanDefinitionDocumentReader.java:308)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseDefaultElement(DefaultBeanDefinitionDocumentReader.java:197)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.parseBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:176)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.doRegisterBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:149)
at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.registerBeanDefinitions(DefaultBeanDefinitionDocumentReader.java:96)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.registerBeanDefinitions(XmlBeanDefinitionReader.java:513)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.doLoadBeanDefinitions(XmlBeanDefinitionReader.java:393)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:336)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:304)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:188)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:224)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:195)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:257)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:128)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:94)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:133)
at org.springframework.context.support.AbstractApplicationContext.obtainFreshBeanFactory(AbstractApplicationContext.java:622)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:518)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:144)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:85)
at com.spring.jbctemplate.transaction.MainApp.main(MainApp.java:12)

transaction.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:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.3.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">

<context:component-scan
    base-package="com.spring.jbctemplate.transaction" />

<bean id="dataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName"
        value="com.mysql.cj.jdbc.Driver" />
    <property name="url"
        value="jdbc:mysql://localhost:3306/SPRING?useSSL=false" />
    <property name="username" value="root" />
    <property name="password" value="root123" /><tx:advice></tx:advice>
</bean>

<bean id="transactionManager"
    class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource"/>
</bean>

<!-- <bean id="studentDaoImp" class="com.spring.jbctemplate.transaction.StudentDaoImp"> -->
<!-- <property name="dataSource" ref="dataSource"/> -->
<!-- </bean> -->

<!-- <tx:advice id="myAdvice" transaction-manager="transactionManager"> -->
<!-- <tx:attributes> -->
<!-- <tx:method name="create" propagation="REQUIRED" -->
<!-- isolation="DEFAULT" read-only="false" /> -->
<!-- </tx:attributes> -->
<!-- </tx:advice> -->

<!-- <aop:config> -->
<!-- <aop:pointcut expression="execution(* com.spring.jbctemplate.transaction.*.create(..))" -->
<!-- id="myPointCut"/> -->
<!-- <aop:advisor advice-ref="myAdvice" pointcut-ref="myPointCut"/> -->
<!-- </aop:config> -->

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

</beans>

StudentDaoImp.java代码:

package com.spring.jbctemplate.transaction;

import java.util.List;
import javax.annotation.Resource;
import javax.sql.DataSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

@Component("studentDaoImp")
public class StudentDaoImp implements StudentDAO {

private DataSource dataSource;
private JdbcTemplate JdbcTemplate;

@Resource(name="dataSource")
public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
    this.JdbcTemplate = new JdbcTemplate(this.dataSource);
}

@Transactional
public void create(String name, Integer age, Integer marks, Integer year){
    String SQL1 = "insert into Student (name, age) values (?, ?)";
    JdbcTemplate.update( SQL1, name, age);
    // Get the latest student id to be used in Marks table
    String SQL2 = "select max(id) from Student";
    int sid = JdbcTemplate.queryForObject(SQL2, Integer.class);
    int i = 10/0;
    String SQL3 = "insert into Marks(sid, marks, year) " + 
            "values (?, ?, ?)";
    JdbcTemplate.update( SQL3, sid, marks, year);
    System.out.println("Created Name = " + name + ", Age = " + age);
}
   public List<StudentMarks> listStudents() {
      String SQL = "select * from Student, Marks where Student.id = Marks.sid";
      List <StudentMarks> studentMarks = JdbcTemplate.query(SQL, new StudentMarksMapper());
      return studentMarks;
   }

}

MainApp.java测试类代码:

package com.spring.jbctemplate.transaction;

import java.util.List;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class MainApp {

public static void main(String[] args) {
    ApplicationContext context = 
             new ClassPathXmlApplicationContext("transaction.xml");
      StudentDAO sd = (StudentDAO)context.getBean("studentDaoImp");     
      System.out.println("------Records creation--------" );
      sd.create("Zara", 11, 99, 2010);
      sd.create("Nuha", 20, 97, 2010);
      sd.create("Ayan", 25, 100, 2011);
      System.out.println("------Listing all the records--------" );
      List<StudentMarks> studentMarks = sd.listStudents();
      for (StudentMarks record : studentMarks) {
         System.out.print("ID : " + record.getId() );
         System.out.print(", Name : " + record.getName() );
         System.out.print(", Marks : " + record.getMarks());
         System.out.print(", Year : " + record.getYear());
         System.out.println(", Age : " + record.getAge());
      }
   }

}

doubleya的主页 doubleya | 菜鸟二级 | 园豆:204
提问于:2018-11-20 12:55
< >
分享
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册