由以下原因引起:org.springframework.beans.NotWritablePropertyException:Bean类的无效属性'dataSource'Bean属性'dataSource'不是wri

时间:2018-07-24 18:05:25

标签: spring spring-jdbc

我关注了诸如Bean property 'myDataSource' is not writable or has an invalid setter method之类的许多链接,但没有解决我的问题。

我收到以下错误:

有人可以指出问题是什么吗?到目前为止,我什么也没做。

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'myBIllingService' defined in class path resource [config/context.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [com.hp.service.MyBIllingService]: Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1493)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1197)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:537)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:475)
    at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:304)
    at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:300)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:195)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:703)
    at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:760)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:482)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
    at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
    at com.hp.App.main(App.java:15)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [com.hp.service.MyBIllingService]: Bean property 'dataSource' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1064)
    at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:922)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:82)
    at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:62)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1489)
    ... 13 more

以下代码:

public class MyBIllingService {
    private JdbcTemplate jdbcTemplate;
    private DataSource dataSource;

    @Autowired
    public MyBIllingService(DataSource dataSource) {
        this.dataSource = dataSource;
        this.jdbcTemplate = new JdbcTemplate(dataSource);
    }
}

context.xml

<context:property-placeholder location="classpath:database.properties" />

    <context:component-scan base-package="com.hp.*" />

    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />

    <!-- connect to ORACLE database -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${spring.datasource.driver-class-name}" />
        <property name="url" value="${spring.datasource.url}" />
        <property name="username" value="${spring.datasource.username}" />
        <property name="password" value="${spring.datasource.password}" />
    </bean>

    <bean id = "myBIllingService" class="com.hp.service.MyBIllingService">
        <property name = "dataSource" ref = "dataSource" />
    </bean>

2 个答案:

答案 0 :(得分:1)

您正在通过XML配置将数据源连接到MyBillingService。为什么还要通过构造函数尝试自动装配?尝试从MyBillingService构造函数中删除自动装配。

答案 1 :(得分:0)

如果您遇到以下问题

org.springframework.beans.NotWritablePropertyException:Bean类[com.rcu.javatpoint.FanDAOImpl]的无效属性'dataSource':Bean属性'dataSource'无法写或具有无效的setter方法。 setter的参数类型是否与getter的返回类型匹配?

确保已为要创建bean id的bean类添加了getter / setter。 添加getter / setter方法时,不会出现此错误。