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

时间:2018-04-16 12:29:30

标签: java spring beancreationexception

我正在我的应用服务层实现Spring Transaction ..

我指的是关于实现spring transaction的编程方式的教程的例子......

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

我跟着每个&那里提到的每一步......但是在xml文件中遇到bean创建的问题..

错误:

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

系统&应用配置: 操作系统:ubuntu 16.0.4 春季版: - 5.0.3 tomcat:9 jdk:1.9
IDE:Eclipse Oxygen 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

1 个答案:

答案 0 :(得分:0)

设置数据源的DataSourceTransactionManager的属性名称在stacktrace dataSource中写入,而不是xxxxxDataSource。你的配置应该如下

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