org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'menuServiceImple': Autowiring of fields failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cxsoft.dao.TMenuDAO com.cxsoft.service.imple.MenuServiceImple.menuDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tMenuDAO' defined in file [E:\home\examSystemV2\examSystem\WEB-INF\classes\spring\spring_Dao.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.cxsoft.dao.TMenuDAO]: Bean property 'sessionFactory' 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.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:243)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:959)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:429)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:728)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:380)
at org.springframework.web.context.ContextLoader.createWebApplicationContext(ContextLoader.java:255)
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:199)
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:45)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4135)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4630)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:785)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:445)
at org.apache.catalina.core.StandardService.start(StandardService.java:519)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:581)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:289)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:414)
Caused by: org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.cxsoft.dao.TMenuDAO com.cxsoft.service.imple.MenuServiceImple.menuDao; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tMenuDAO' defined in file [E:\home\examSystemV2\examSystem\WEB-INF\classes\spring\spring_Dao.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.cxsoft.dao.TMenuDAO]: Bean property 'sessionFactory' 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.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:435)
at org.springframework.beans.factory.annotation.InjectionMetadata.injectFields(InjectionMetadata.java:105)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessAfterInstantiation(AutowiredAnnotationBeanPostProcessor.java:240)
... 31 more
Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'tMenuDAO' defined in file [E:\home\examSystemV2\examSystem\WEB-INF\classes\spring\spring_Dao.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.cxsoft.dao.TMenuDAO]: Bean property 'sessionFactory' 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:1279)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1010)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:472)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$1.run(AbstractAutowireCapableBeanFactory.java:409)
at java.security.AccessController.doPrivileged(Native Method)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:380)
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:264)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:222)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:164)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.findAutowireCandidates(DefaultListableBeanFactory.java:671)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:610)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 33 more
Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.cxsoft.dao.TMenuDAO]: Bean property 'sessionFactory' 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:801)
at org.springframework.beans.BeanWrapperImpl.setPropertyValue(BeanWrapperImpl.java:651)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:78)
at org.springframework.beans.AbstractPropertyAccessor.setPropertyValues(AbstractPropertyAccessor.java:59)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.applyPropertyValues(AbstractAutowireCapableBeanFactory.java:1276)
... 46 more
网上搜了下说是:
1。必须在实现类中以@Service或@Component注解才行。 
2。自动写入的时候把接口写成实现类了也会出现异常 。(但是我检查代码并没有出现这类情况) 
请各位帮忙指点  谢谢`~

解决方案 »

  1.   

    menuServiceImple
    看看你注入的名字和这个一样吗 不一样肯定会报错的啊
      

  2.   

    Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory检查你的配置文件,和发下你的配置文件。
      

  3.   

    spring_Dao.xml配置文件中tMenuDAO虽然配置了,但是找不到set/get方法或set/get方法不对你看看tMenuDAO这个是在哪里进行注入的,是哪一个类的属性?
      

  4.   

    另外:检查sessionFactory
    Does the parameter type of the setter match the return type of the getter?
      

  5.   

    任何组件都必须用注解,加入到Ioc窗口中,就如同你在xml中写一个<bean>标签一样你的Dao都没有加入容器,请问在service直接用,spring知道从那里给你偷一个过来??
      

  6.   

    spring.xml中:<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:tx="http://www.springframework.org/schema/tx"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"
    default-autowire="byName" default-lazy-init="false">
    <context:annotation-config />
    <context:component-scan base-package="com.cxsoft"/>
    <bean id="propertyConfigurer"
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
    <property name="locations" >
    <list>
    <value>classpath:property/property*.properties</value>
    </list>
    </property>
    <!-- 
    <property name="location" value="/WEB-INF/classes/resource/jdbc.properties" />
     -->
    </bean>


    <bean id="MYSQLDataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${driverClass}" />
    <property name="url" value="${jdbcUrl}" />
    <property name="username" value="${user}" />
    <property name="password" value="${password}" />
    </bean>
    <bean id="examSessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="dataSource">
    <ref bean="MYSQLDataSource" />
    </property>
    <property name="hibernateProperties">
    <props>
    <prop key="hibernate.dialect">
    org.hibernate.dialect.MySQLDialect
    </prop>
    </props>
    </property>
    <property name="mappingLocations">
    <value>
    classpath:/com/cxsoft/model/*hbm.xml
    </value>
    </property>
    </bean>


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

    <!-- 配置事务切面 -->
    <tx:advice id="transactionAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="find*" read-only="true" />
    <tx:method name="get*" read-only="true" />
    <tx:method name="query*" read-only="true" />
    <tx:method name="select*" read-only="true" />
    <tx:method name="*" read-only="false" />
    </tx:attributes>
    </tx:advice> <aop:config>
    <aop:pointcut
    expression="execution(* com.cxsoft.*..*.service.*Service*.*(..))"
    id="txPoint" />
    <aop:advisor advice-ref="transactionAdvice"
    pointcut-ref="txPoint"/>
    </aop:config>
    </beans>
    spring_Dao.xml中: <bean id="tMenuDAO" class="com.cxsoft.dao.TMenuDAO">
    <property name="sessionFactory">
    <ref bean="examSessionFactory" />
    </property>
    </bean>
      

  7.   


    org.springframework.beans.NotWritablePropertyException: Invalid property 'sessionFactory' of bean class [com.cxsoft.dao.TMenuDAO]: Bean property 'sessionFactory' is not writable or has an invalid setter method. Does the parameter type of the setter match the return type of the getter?这个是重点...
      

  8.   

    'sessionFactory' is not writable or has an invalid setter method
      

  9.   

    在menuServiceImple下面的属性sessionFactory上加注解    @Autowired。或者是加一个setter方法
      

  10.   

    spring_Dao.xml配置文件中tMenuDAO虽然配置了,但是找不到set/get方法或set/get方法不对你看看tMenuDAO这个是在哪里进行注入的,是哪一个类的属性? 
      

  11.   

    错误提示很明显,检查下DAO中对sessionFactory这个属性注入时要用到的set方法有没有