我是用了一个泛型dao接口和泛型dao 实现,
然后定义了接口
public interface ImUserDeptDao extends GenericDao<ImUserDeptEntity, Integer> {
}dao 实现
package com.target.dao;public class ImUserDeptDaoImp extends GenericDaoImpl<ImUserDeptEntity, Integer> implements ImUserDeptDao {
}在dao bean 的配置中定义了
    <bean id="imUserDeptDaoTarget" class="com.target.dao.ImUserDeptDaoImp">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <bean id="imUserDeptDao" parent="transactionProxy">
        <property name="target" ref="imUserDeptDaoTarget"/>
    </bean> 为什么必须通过这两个配置,才能够访问到imUserDeptDao呢
害得我不知道该怎么去把dao bean 配置用@Repository去标注,请高人帮忙!!!!!数据源配置情况:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       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">
    <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/spring-jdbc.properties</value>
            </list>
        </property>
    </bean>    <bean id="dataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource" init-method="">
        <property name="alias">
            <value>dataSource</value>
        </property>
        <property name="driver">
            <value>${jdbc.driver}</value>
        </property>
        <property name="driverUrl">
            <value>${jdbc.url}</value>
        </property>
        <property name="user">
            <value>${jdbc.username}</value>
        </property>
        <property name="password">
            <value>${jdbc.password}</value>
        </property>
        <property name="maximumConnectionCount">
            <value>${jdbc.maxPoolSize}</value>
        </property>
        <property name="minimumConnectionCount">
            <value>${jdbc.minPoolSize}</value>
        </property>
        <property name="houseKeepingSleepTime">
            <value>${jdbc.maxIdleTime}</value>
        </property>
        <property name="prototypeCount">
            <value>${jdbc.prototypeCount}</value>
        </property>
        <property name="trace">
            <value>${jdbc.trace}</value>
        </property>
        <property name="verbose">
            <value>${jdbc.verbose}</value>
        </property>
        <property name="maximumActiveTime">
            <value>${jdbc.maximumActiveTime}</value>
        </property>
        <property name="houseKeepingTestSql">
            <value>${jdbc.houseKeepingTestSql}</value>
        </property>
        <property name="simultaneousBuildThrottle">
            <value>${jdbc.simultaneousBuildThrottle}</value>
        </property>
        <property name="maximumConnectionLifetime">
            <value>${jdbc.maximumConnectionLifetime}</value>
        </property>
        <property name="delegateProperties">
            <value>${jdbc.delegateProperties}</value>
        </property>
    </bean>    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
        <property name="annotatedClasses">
            <list>
                <value>com.target.model.ImUserEntity</value>
                <value>com.target.model.ImUserDeptEntity</value>
            </list>
        </property>
        <!--<property name="mappingDirectoryLocations">-->
<!--<list>-->
<!--<value>classpath:/model</value>-->
<!--</list>-->
<!--</property>-->
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.show_sql">true</prop>
                <prop key="hibernate.format_sql">true</prop>
            </props>
        </property>
    </bean>    <!--配置声明式事务-->
    <bean id="transactionManager"
          class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"/>
    </bean>
    <!--配置事务代理工厂-->
    <bean id="transactionProxy"
          class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
          lazy-init="true" abstract="true">
        <!-- 配置事务管理器 -->
        <property name="transactionManager" ref="transactionManager"/>
        <property name="proxyTargetClass" value="true"/>         
        <!-- 配置事务属性 -->
        <property name="transactionAttributes">
            <props>
                <prop key="*">PROPAGATION_REQUIRED</prop>
            </props>
        </property>
    </bean>
</beans>

解决方案 »

  1.   

    就想问
      <bean id="imUserDeptDaoTarget" class="com.target.dao.ImUserDeptDaoImp">
      <property name="sessionFactory" ref="sessionFactory"/>
      </bean>
      <bean id="imUserDeptDao" parent="transactionProxy">
      <property name="target" ref="imUserDeptDaoTarget"/>
      </bean>
    这个能不能合成一个bean,就可以访问到imUserDeptDao,如果可以,应该怎样合并?
    或者我在
    public class ImUserDeptDaoImp extends GenericDaoImpl<ImUserDeptEntity, Integer> implements 
    之前应该怎样标注,达到同样的效果?
      

  2.   

    合并?不太明白你的意思,但是要是你用annotation的话,你就可以在你的service层直接注入你想用的dao就行了,在dao层,你可以直接对ImUserDeptDaoImp 进行注解就行了。这样写主要是因为,由于dao层大部分的dao里头都有CRUD方法,而这些方法抽取出来是可以重用的,所以就把那些公有的方法抽取出来,作为父类,让所有的子类dao去继承它,达到代码重用的目的。
      

  3.   

    <bean id="imUserDeptDao" parent="transactionProxy" class="com.target.dao.ImUserDeptDaoImp">
      <property name="sessionFactory" ref="sessionFactory"/>
     </bean>
    这样试试呢
      

  4.   

    楼上的,这样不行
    严重: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imUserDeptDao' defined in ServletContext resource [/WEB-INF/spring-context-dao.xml]: Initialization of bean failed; nested exception is org.springframework.beans.InvalidPropertyException: Invalid property 'transactionManager' of bean class [com.target.dao.ImUserDeptDaoImp]: No property 'transactionManager' found
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:480)
    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:221)
    at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:261)
    at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:185)
      

  5.   

    5楼的kaida_7,我如果直接把这段代码删了加上annotation
    @Repository("imUserDeptDao")
    public class ImUserDeptDaoImp extends GenericDaoImpl<ImUserDeptEntity, Integer> implements ImUserDeptDao {
    }
    就会报
    严重: Context initialization failed
    org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'imUserDeptDao' defined in file [F:\my web\Test\web\WEB-INF\classes\com\target\dao\ImUserDeptDaoImp.class]: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: 'sessionFactory' or 'hibernateTemplate' is required
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.initializeBean(AbstractAutowireCapableBeanFactory.java:1337)
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:473)
    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:221)
    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:729)
    at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:381)
    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.addChildInternal(ContainerBase.java:791)
    at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
    at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:546)
    at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:637)