简体   繁体   English

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

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

I am implementing Spring Transaction in my application service layer.. 我正在我的应用程序服务层中实现Spring Transaction。

I was referring to example on tutorials point for programmatic way of implementing spring transaction... 我指的是教程中的示例,以编程方式实现spring事务。

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

I followed each & every step mentioned over there....but getting issue with bean creation in the xml file.. 我遵循了上面提到的每个步骤.....但是在xml文件中创建bean时遇到了问题。

Error: 错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'transactionManager' defined in class path resource [database/spring.xml]: Error setting property values; 线程“主”中的异常org.springframework.beans.factory.BeanCreationException:创建类路径资源[database / spring.xml]中定义的名称为'transactionManager'的bean时出错:设置属性值时出错; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [org.springframework.jdbc.datasource.DataSourceTransactionManager]: Bean property 'dataSource' is not writable or has an invalid setter method. 嵌套的异常是org.springframework.beans.NotWritablePropertyException:Bean类[org.springframework.jdbc.datasource.DataSourceTransactionManager]的无效属性'dataSource':Bean属性'dataSource'是不可写的或具有无效的setter方法。 Does the parameter type of the setter match the return type of the getter? setter的参数类型是否与getter的返回类型匹配?

system & application configuration: OS: ubuntu 16.0.4 spring version: - 5.0.3 tomcat: 9 jdk: 1.9 系统和应用程序配置:操作系统:ubuntu 16.0.4春季版本:-5.0.3 Tomcat:9 jdk:1.9
IDE: Eclipse Oxygen 3 IDE:Eclipse氧气3

It will be very helpful for me if any one can give me the solution... 如果有人可以给我解决方案,这对我将非常有帮助。

<?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

The property name of DataSourceTransactionManager that set the datasource is as writen in the stacktrace dataSource and not xxxxxDataSource . 设置数据源的DataSourceTransactionManager的属性名称是在stacktrace dataSource中写入的,而不是xxxxxDataSource。 you config should be as follow 您的配置应如下

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.springframework.beans.NotWritablePropertyException。 无效属性<blah>不可写或具有无效的 setter 方法</blah> - org.springframework.beans.NotWritablePropertyException. Invalid property <blah> is not writable or has an invalid setter method Bean属性“ bagBisDomainService”不可写或具有无效的setter方法 - Bean property 'bagBisDomainService' is not writable or has an invalid setter method 春季:Bean属性不可写或具有无效的setter方法 - Spring: Bean property is not writable or has an invalid setter method Bean属性“transactionManagerBeanName”不可写或具有无效的setter方法 - Bean property 'transactionManagerBeanName' is not writable or has an invalid setter method Bean属性xxxDAO无法写或具有无效的setter方法 - Bean property xxxDAO is not writable or has an invalid setter method Bean属性“ channelIdentifierMap”不可写或具有无效的setter方法 - Bean property 'channelIdentifierMap' is not writable or has an invalid setter method Spring Bean属性'xxx'不可写或具有无效的setter方法 - Spring Bean property 'xxx' is not writable or has an invalid setter method Bean 属性“sessionFactory”不可写或具有无效的 setter 方法 - Bean property 'sessionFactory' is not writable or has an invalid setter method bean类的属性&#39;profileManager&#39;无效。 Bean属性“profileManager”不可写或具有无效的setter方法 - Invalid property 'profileManager' of bean class. Bean property 'profileManager' is not writable or has an invalid setter method Bean类的无效属性不可写或无效的setter方法 - Invalid property of bean class is not writable or an invalid setter method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM