本帖最后由 cart55free99 于 2011-06-05 15:19:15 编辑

解决方案 »

  1.   


        ClassPathXmlApplicationContext c = new ClassPathXmlApplicationContext(
                    "applicationContext.xml");
        TbRoleDAO roleDAO = (TbRoleDAO) c.getBean("roleDAO");相当于 TbRoleDAO roleDAO = new TbRoleDAO(); 之后就和spring 没有关系了,你还是看看
    TbRoleDAO吧
      

  2.   


        <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="configLocation" value="classpath:hibernate.cfg.xml" />
            <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
        </bean>
    这一段配置是在干嘛呢? 我搞不明白。SessionFactory就算是配好了?如果加上事务 是这样的么?<?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
    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-2.0.xsd
    http://www.springframework.org/schema/aop
    http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
    http://www.springframework.org/schema/tx
    http://www.springframework.org/schema/tx/spring-tx-2.0.xsd"> <bean id="sessionFactory"
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
    <property name="configLocation" value="classpath:hibernate.cfg.xml" />
    <property name="configurationClass" value="org.hibernate.cfg.AnnotationConfiguration" />
    </bean>
    <bean id="txManager"
    class="org.springframework.orm.hibernate3.HibernateTransactionManager">
    <property name="sessionFactory" ref="sessionFactory" />
    </bean>
    <tx:advice id="txAdvice" transaction-manager="txManager">
    <tx:attributes>
    <tx:method name="save*" propagation="REQUIRED" />
    <tx:method name="delete*" propagation="REQUIRED" />
    <tx:method name="update*" propagation="REQUIRED" />
    <tx:method name="merge*" propagation="REQUIRED" />
    <tx:method name="find*" propagation="REQUIRED" />
    <tx:method name="get*" propagation="SUPPORTS" />
    <tx:method name="search*" propagation="SUPPORTS" />
    <tx:method name="show*" propagation="SUPPORTS" />
    </tx:attributes>
    </tx:advice>
    <aop:config>
    <aop:pointcut id="interceptorPointCuts" expression="execution(* dao.impl.*.*(..))" />
    <aop:advisor advice-ref="txAdvice" pointcut-ref="interceptorPointCuts" />
    </aop:config>
    <!--事务管理配置结束 --> <bean id="baseDAO" class="dao.BaseHibernateDAO"></bean>
    <bean id="roleDAO" class="dao.TbRoleDAO"></bean>
    </beans>
      

  3.   

    有了新的问题 <bean id="baseDAO" class="dao.BaseHibernateDAO">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    <bean id="permissionDAO" class="dao.impl.TbPermissionDAO">
    <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>
    必须这样才行TbPermissionDAO 是继承了 BaseHibernateDAO 的
    本以为对父类注入SessionFactory后 子类就不用注入了。但是不行啊
    子类(TbPermissionDAO 还是注要入一遍 SessionFactory  )
    否则说 'sessionFactory' or 'hibernateTemplate' is required我记得别人说可以通过给父DAO注入SessionFactory  子类就不用注入了的
      

  4.   


    看了你的  不错 但是我的疑问还是没有解决。
    我有一个DAO父类 已经注入了SessionFactory , 结果子类还是要再次注入才行。。
    否则服务器启动都错但是我记得是可以通过给父类注入 ,子类就不用注入的。。要不然配置文件会很长很长很长
      

  5.   

    SessionFactory 的主要作用是用来创建hibernateTemplate, 
    当spring这个bean容器中有了hibernateTemplate时,
    我们只要在想用到它的任何地方写上:@Resource  
    private HibernateTemplate hibernateTemplate;就可以用了