我关注了很多像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>
发布于 2018-07-24 19:20:16
您通过XML配置将dataSource连接到您的MyBillingService。为什么你还要尝试通过构造器来自动连接呢?试着从你的MyBillingService构造器中删除自动连接。
发布于 2020-10-08 14:36:35
如果您正面临以下问题
org.springframework.beans.NotWritablePropertyException: bean类com.rcu.javatpoint.FanDAOImpl的setter属性'dataSource‘无效: Bean属性'dataSource’不可写或具有无效的setter方法。setter的参数类型是否与getter的返回类型匹配?
确保您已经为要创建bean id的bean类添加了getter/setter。当你添加getter/setter方法时,你不会得到这个错误。
https://stackoverflow.com/questions/51505006
复制相似问题