Bean属性xxxDAO不可写或具有无效的setter方法

时间:2015-06-21 13:43:10

标签: java xml spring hibernate spring-mvc

我知道这个问题已被多次询问过。但我找不到我的解决方案。

我正在使用带休眠的spring。 User和UserWord之间存在一对多映射的连接 我为其他模型做了同样的逻辑,一切都很好。 但是当我进行映射时,我收到了错误。

我的spring.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <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:tx="http://www.springframework.org/schema/tx"
        xmlns:context="http://www.springframework.org/schema/context"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
        http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

        <tx:annotation-driven transaction-manager="txManager"/> 

        <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="com.mysql.jdbc.Driver" />
        <property name="url" value="jdbc:mysql://localhost:3306/springdb" />
        <property name="username" value="xxx" />
        <property name="password" value="xxx" />
        </bean>

        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource" />
            <property name="mappingResources">
                <list>
                    <value>com/memorize/dao/mapping/Users.hbm.xml</value>
                    <value>com/memorize/dao/mapping/Word.hbm.xml</value>
                    <value>com/memorize/dao/mapping/UserWord.hbm.xml</value>
                </list>
            </property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                    <prop key="hibernate.hbm2ddl.auto">update</prop>
                </props>
            </property>
        </bean>

        <!-- <bean id="genericDAO" class="com.memorize.dao.impl.GenericDAOImpl">
            <constructor-arg>
                <value>com.memorize.model.Users</value>
            </constructor-arg>
        </bean>-->

        <bean id="usersDAO" class="com.memorize.dao.impl.UsersDAOImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>

        <bean id="userService" class="com.memorize.service.impl.UsersServiceImpl">
            <property name="usersDAO" ref="usersDAO"></property>
        </bean>

        <bean id="wordDAO" class="com.memorize.dao.impl.WordDAOImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>

        <bean id="wordService" class="com.memorize.service.impl.WordServiceImpl">
            <property name="wordDAO" ref="wordDAO"></property>
        </bean>

        <bean id="userWordDAO" class="com.memorize.dao.impl.UserWordDAOImpl">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>

        <bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl">
            <property name="userWordDAO" ref="userWordDAO"></property>
        </bean>

        <bean id="txManager"
            class="org.springframework.orm.hibernate4.HibernateTransactionManager">
            <property name="sessionFactory" ref="sessionFactory" />
        </bean>
    </beans>

UserWord.hbm.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

        <hibernate-mapping>
            <class name="com.memorize.model.UserWord" table="userwords" catalog="springdb">
                <id name="userWordId" type="java.lang.Integer">
                    <column name="ID" />
                    <generator class="identity" />
                </id>
                <many-to-one name="user" class="com.memorize.model.Users" fetch="select">
                    <column name="USER_ID" not-null="true"></column>
                </many-to-one>
            </class>
        </hibernate-mapping>

UserWordServiceImpl.java

        package com.memorize.service.impl;

        import org.springframework.transaction.annotation.Transactional;

        import com.memorize.dao.UserWordDAO;
        import com.memorize.model.UserWord;
        import com.memorize.service.UserWordService;

        public class UserWordServiceImpl implements UserWordService{

            private UserWordDAO userWordDao;

            public void setUserWordDao(UserWordDAO userWordDao) {
                this.userWordDao = userWordDao;
            }

            @Override
            @Transactional
            public void saveOrUpdateUserWord(UserWord uw) {
                // TODO Auto-generated method stub
                this.userWordDao.saveOrUpdate(uw);
            }
        }

UserWordDAOImpl.java

        package com.memorize.dao.impl;

        import org.hibernate.Session;
        import org.hibernate.SessionFactory;
        import org.springframework.beans.factory.annotation.Autowired;

        import com.memorize.dao.UserWordDAO;
        import com.memorize.model.UserWord;

        public class UserWordDAOImpl implements UserWordDAO{

            @Autowired
            private SessionFactory sessionFactory;

            public void setSessionFactory(SessionFactory sf){
                this.sessionFactory = sf;
            }

            @Override
            public void saveOrUpdate(UserWord uw) {
                // TODO Auto-generated method stub
                Session session = this.sessionFactory.getCurrentSession();
                session.persist(uw);
            }
        }

Users.hbm.xml

        <?xml version="1.0" encoding="UTF-8"?>
        <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
        "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

        <hibernate-mapping>
            <class name="com.memorize.model.Users" table="users" catalog="springdb">
                <id name="userId" type="java.lang.Integer">
                    <column name="ID" />
                    <generator class="identity"/>
                </id>
                <property name="userName" type="string">
                    <column name="USER_NAME" not-null="true" unique="true" />
                </property>

                <set name="userWords" table="userwords" fetch="select">
                    <key>
                        <column name="ID" not-null="true"></column>
                    </key>
                    <one-to-many class="com.memorize.model.UserWord"/>
                </set>
            </class>
        </hibernate-mapping>

这是错误:

        Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userWordService' defined in class path resource [spring/config/spring.xml]: Error setting property values; nested exception is org.springframework.beans.NotWritablePropertyException: Invalid property 'userWordDAO' of bean class [com.memorize.service.impl.UserWordServiceImpl]: Bean property 'userWordDAO' is not writable or has an invalid setter method. Did you mean 'userWordDao'?
        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:302)
        at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:228)
        at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:298)
        at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
        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.memorize.common.Test.main(Test.java:18)
        Caused by: org.springframework.beans.NotWritablePropertyException: Invalid property 'userWordDAO' of bean class [com.memorize.service.impl.UserWordServiceImpl]: Bean property 'userWordDAO' is not writable or has an invalid setter method. Did you mean 'userWordDao'?
        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

    Thanks for helping

1 个答案:

答案 0 :(得分:1)

您的媒体资源名称userWordDao位于课程UserWordServiceImpl中,而您正试图设置属性userWordDAO(不匹配),但不存在。所以在spring.xml中更改

<bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl">
            <property name="userWordDAO" ref="userWordDAO"></property>
        </bean>

<bean id="userWordService" class="com.memorize.service.impl.UserWordServiceImpl">
            <property name="userWordDao" ref="userWordDAO"></property>
        </bean>