日期:2014-05-20  浏览次数:20939 次

spring配置文件的注入问题
求解:
这样写的时候没有错
XML code
<bean id="organuserServices"
        class="com.dianxue.services.impl.OrganuserServices">
        <property name="entityDao">
                <ref bean="organuserDAO"/>
        </property>
    </bean>



这样写报错了:
XML code
    <bean id="organuserServices"
        class="com.dianxue.services.impl.OrganuserServices">
        <property name="entityDao">
            <list>
                <ref bean="organuserDAO"/>
            </list>
        </property>
    </bean>



org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'organuserServices' defined in ServletContext resource [/WEB-INF/classes/dataAccessContext.xml]: Initialization of bean failed; nested exception is org.springframework.beans.TypeMismatchException: Failed to convert property value of type [java.util.ArrayList] to required type [com.core.DAO.EntityDao] for property 'entityDao'; nested exception is java.lang.IllegalArgumentException: Cannot convert value of type [java.util.ArrayList] to required type [com.core.DAO.EntityDao] for property 'entityDao': no matching editors or conversion strategy found

------解决方案--------------------
噢。。。看错了
 <list>
<ref bean="organuserDAO"/>
</list>
这个就是List类型
------解决方案--------------------
Java code

<bean id="organuserServices"
        class="com.dianxue.services.impl.OrganuserServices">
        <property name="entityDao">
             <ref bean="organuserDAO"/>
        </property>
        <property name="第二个Dao">
             <ref bean="第二个Dao"/>
        </property>
    </bean>

------解决方案--------------------
探讨

Java code

public void setEntityDao(EntityDao entityDao) {
this.entityDao = entityDao;
}



那是不是把setEntityDao的参数改成list呢

------解决方案--------------------
探讨

引用:




可是把参数改成list的话, entityDao 就变成list类型了,就不能entityDao.selectByID 这样调用了

------解决方案--------------------
探讨

引用:

引用:




可是把参数改成list的话, entityDao 就变成list类型了,就不能entityDao.selectByID 这样调用了

怎么安排都是逻辑上来说的,我只是说你如果要用<list></list>那你set的参数必须是List<>来匹配
业务逻辑如果需要你用……