NotWritablePropertyException: Bean 属性 'dataSource' 不可写或具有无效的 setter 方法

NotWritablePropertyException: Bean property 'dataSource' is not writable or has an invalid setter method

我正在我的应用程序服务层中实现 Spring 事务..

我指的是关于实现 spring 事务的编程方式的教程点示例...

https://www.tutorialspoint.com/spring/programmatic_management.htm

我遵循了那里提到的每一个步骤....但是在 xml 文件中创建 bean 时遇到问题..

错误:

线程 "main" 中出现异常 org.springframework.beans.factory.BeanCreationException:创建名称为 'transactionManager' 的 bean 在 class 路径资源 [database/spring.xml 中定义时出错]:错误设置属性值;嵌套异常是 org.springframework.beans.NotWritablePropertyException: bean class [org.springframework.jdbc.datasource.DataSourceTransactionManager] 的无效 属性 'dataSource': Bean 属性 'dataSource' 不可写或有一个无效的 setter 方法。 setter的参数类型是否匹配getter的return类型?

系统和应用程序配置: OS: ubuntu 16.0.4 spring 版本:- 5.0.3 tomcat: 9 jdk: 1.9
IDE:蚀氧 3

如果有人能给我解决方案,那将对我很有帮助...

<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">

       <!-- ********************* Initialization for Inventory database -->

         <bean id="dataSource"
            class="org.springframework.jdbc.datasource.DriverManagerDataSource">
            <property name="driverClassName" value="com.mysql.jdbc.Driver" />
            <property name="url" value="jdbc:mysql://localhost:3306/myDatabase" />
            <property name="username" value="root" />
            <property name="password" value="root" />
        </bean>

        <!--*********************  Initialization for TransactionManager -->
        <bean class="org.springframework.jdbc.datasource.DataSourceTransactionManager" id="transactionManager">
          <property name="dataSource" ref="dataSource"></property>    
       </bean> 

    </beans>


public class MyService implements InventoryServiceIface{

    private MyDAO dao;

    private PlatformTransactionManager transactionManager;

    public void setTransactionManager(PlatformTransactionManager transactionManager) {

        this.transactionManager = transactionManager;
    }

    public void setDAO(MyDAO dao) {

        this.dao = dao;
    }


    public Student saveStudent(Student tudent) throws ServiceException{

              validate(Student); // validate the data inside party object 

              TransactionStatus status = beginTransaction(); // begin database transaction

              try 
              {
                  rollbackTransaction();
                  Student =  dao.saveStudent(student); 
              }
              catch(ServiceException e)
              {
                   rollbackTransaction(status);
                   throw e;
              }
              catch(Exception e)
              {
                   rollbackTransaction(status);
                   throw new ServiceException(e);
              }

               commitTransaction(status);

          return student;
    }

    public TransactionStatus beginTransaction()

    {
        System.out.println("TRANSACTION BEGINS....");
        return transactionManager.getTransaction(new DefaultTransactionDefinition());

    }

    public void rollbackTransaction(TransactionStatus status)

    {
        System.out.println("ROLL BACK....");
        transactionManager.rollback(status);

    }

    public void commitTransaction(TransactionStatus status)

    {
        System.out.println("TRANSACTION COMMITTED....");
        transactionManager.commit(status);

    }

}// End of Class

设置数据源的 DataSourceTransactionManager 的 属性 名称与堆栈跟踪数据源中所写的相同,而不是 xxxxxDataSource 。你的配置应该如下

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