Java Bean属性数据源在Spring中不可写+;冬眠3
我搜索了很多地方,但没有找到以下错误的解决方案:Java Bean属性数据源在Spring中不可写+;冬眠3,java,Java,我搜索了很多地方,但没有找到以下错误的解决方案: Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDaoImpl' defined in class path resource [spring.xml]: Error setting property values; nested exception
Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'hibernateDaoImpl' defined in class path resource [spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [com.springhibernate.example.HibernateDaoImpl]: 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:1385)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1107)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:585)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)
at com.springhibernate.example.SpringHibernateDemo.main(SpringHibernateDemo.java:13)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'dataSource' of bean class [com.springhibernate.example.HibernateDaoImpl]: 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:1057)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:920)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1382)
... 13 more
线程“main”org.springframework.beans.factory.BeanCreationException中的异常:创建在类路径资源[spring.xml]中定义的名为“hibernateDaoImpl”的bean时出错:设置属性值时出错;嵌套异常为org.springframework.beans.NotWritablePropertyException:bean类[com.springhibernate.example.HibernateDoImpl]的无效属性“dataSource]:bean属性“dataSource”不可写或具有无效的setter方法。setter的参数类型与getter的返回类型匹配吗?
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1385)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1107)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:516)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:455)
位于org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:294)
位于org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
位于org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
位于org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
位于org.springframework.beans.factory.support.DefaultListableBeanFactory.PreInstanceSingleton(DefaultListableBeanFactory.java:585)
位于org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:913)
位于org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:139)
位于org.springframework.context.support.ClassPathXmlApplicationContext。(ClassPathXmlApplicationContext.java:83)
位于com.springhibernate.example.SpringHibernateDemo.main(SpringHibernateDemo.java:13)
原因:org.springframework.beans.NotWritablePropertyException:bean类[com.springhibernate.example.HibernateDoImpl]的属性“dataSource”无效:bean属性“dataSource”不可写或具有无效的setter方法。setter的参数类型与getter的返回类型匹配吗?
位于org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:1057)
位于org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:920)
位于org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:76)
位于org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:58)
位于org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1382)
... 还有13个
例外情况来自以下spring xml文件配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
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-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:annotation-config/>
<context:component-scan base-package="com.springhibernate.example"/>
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"/>
<property name="url" value="jdbc:mysql://localhost:3306/HibernateTest"/>
<property name="username" value="root"/>
<property name="password" value="admin"/>
</bean>
<bean id="hibernateDaoImpl" class="com.springhibernate.example.HibernateDaoImpl">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"/>
<property name="packagesToScan" value="com.springhibernate.model"/>
<property name="hibernateProperties">
<props>
<prop key="dialect">
org.hibernate.dialect.MySQLDialect
</prop>
</props>
</property>
</bean>
</beans>
org.hibernate.dialogue.mysqldialogue
还没有收到你的回音,但我要冒险一试
通常,对于Hibernate DAO对象,您需要的是sessionFactory而不是数据源。因此,为DAO提供一种设置sessionFactory的方法(例如,setter或构造函数-我更喜欢构造函数,因为它不需要是可变的)
配置
<bean id="hibernateDaoImpl" class="com.springhibernate.example.HibernateDaoImpl">
<constructor-arg ref="sessionFactory"/>
</bean>
让我们看看com.springhibernate.example.HibernateDaoImpl,它说“Bean属性”数据源“不可写…”让我把它分部分发布,它不会一次全部运行。第一部分如下:
<bean id="hibernateDaoImpl" class="com.springhibernate.example.HibernateDaoImpl">
<constructor-arg ref="sessionFactory"/>
</bean>