spring整合hibernate的sessionFactory,报以下错误 10C

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'customerServiceImpl' defined in class path resource [applicationContext.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'customerDaoImpl' of bean class [cn.itcast.ssh.service.impl.CustomerServiceImpl]: Bean property 'customerDaoImpl' 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:1518)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1226)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:306)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:302)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:839)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:538)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.(ClassPathXmlApplicationContext.java:83)
at cn.itcast.ssh.spring_sessionFactory.Spring_SessionFactory.test(Spring_SessionFactory.java:22)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:69)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:48)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:292)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:86)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:459)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192)
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'customerDaoImpl' of bean class [cn.itcast.ssh.service.impl.CustomerServiceImpl]: Bean property 'customerDaoImpl' 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.createNotWritablePropertyException(BeanWrapperImpl.java:231)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:423)
at org.springframework.beans.AbstractNestablePropertyAccessor.setPropertyValue(AbstractNestablePropertyAccessor.java:280)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:95)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:75)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1514)
... 36 more

spring的配置文件如下
<?xml version="1.0" encoding="UTF-8"?>
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.xsd">

<!-- 1.配置查询所有客户的bean对象,注入spring管理的sessionFactory对象 -->
<bean id="customerDaoImpl" class="cn.itcast.ssh.dao.impl.CustomerDaoImpl">
  <property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<bean id="customerServiceImpl" class="cn.itcast.ssh.service.impl.CustomerServiceImpl">
    <property name="customerDaoImpl" ref="customerDaoImpl"></property>
</bean>

<!-- 2.spring管理action的创建和service层注入 -->
<bean id="findAllCustomer" class="cn.itcast.ssh.web.action.CustomerAction"
    scope="prototype">
    <property name="customerServiceImpl" ref="customerServiceImpl"></property>
</bean>

<!-- 3.在spring配置sessionFactory,注入到dao中 ,就是在LocalSessionFactoryBean类中注入一个属性configLocation,其值value就是引用hibernate的sessionFactory-->
<bean id="sessionFactory" class="org.springframework.orm.hibernate5.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>

** 测试类代码如下**
package cn.itcast.ssh.spring_sessionFactory;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import cn.itcast.ssh.domain.Customer;
import cn.itcast.ssh.service.CustomerService;
import cn.itcast.ssh.service.impl.CustomerServiceImpl;

/**

  • @author Hongqiankun 测试spring管理sessionFactory的创建,是否能用 1.手动加载spring的配置文件
  • 2.获取serviceImpl的bean对象,调用save方法,由于save中使用了sessionFactory
  • 所以,如果spring没能成功注入sessionFactory,则程序保存,无法将新的客户信息录入到数据库;
  • */
    public class Spring_SessionFactory {
    @Test
    public void test() {
    //1.加载spring的配置文件,获得CustomerServiceImpl对象
    ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    CustomerService customerServiceImpl = (CustomerServiceImpl) context.getBean("customerServiceImpl");

    //2.创建新的customer
    Customer cus = new Customer();
    cus.setCustName("北方科技2");
    cus.setCustAddress("北京");
    cus.setCustIndustry("科技公司");

    cus.setCustPhone("13214132344");
    cus.setCustSource("推广");
    cus.setCustLevel("普通客户");
    
    //3.调用saveCustomer方法
    customerServiceImpl.saveCustomer(cus);
    

    }
    }

0

6个回答

spring容器在创建customerDaoImpl这个bean实例的时候出错,仔细检查一下你这实体类,是不是有get方法和set方法的返回类型不一样的

0
srjzh
sunruijie01 回复qq_16723097:还没有解决?
一年多之前 回复
lin595052817
林-林 回复qq_16723097: 看我的回复 你加上去
一年多之前 回复
qq_16723097
qq_16723097 实在是找不到错误,我就在spring的配置文件中,引入的sessionfactory,就不行了
一年多之前 回复
qq_16723097
qq_16723097 给你完成的程序,你能帮我测试下么?
一年多之前 回复

图片说明get set方法返回值类型

0

CustomerServiceImpl 中添加
public void setCustomerDaoImpl(CustomerDaoImpl customerDaoImpl){
this.customerDaoImpl = customerDaoImpl;
}

public CustomerDaoImpl getCustomerDaoImpl(){
return this.customerDaoImpl;
}
你应该好好了解spring ioc 注入模式 接口 setter方法 构造方法 三种方式进行注入 你看看类里面啥也没有怎么注入使用呀

0
qq_16723097
qq_16723097 有的,我在CustomerServiceImpl 使用的set注入
一年多之前 回复

检查下get 和set 方法的返回值类型

0

get 和set 方法的返回值

0

get 和set方法,或者你的spring 配置里加入根据类型创建实例。

0
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!