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

spring 泛型DAO配置
我写了个BaseDAO有泛型的:
public abstract class BaseDAO<T, ID extends Serializable> implements IBaseDAO<T, ID>
请问我在applicationContext.xml里要怎么配置BaseDAO这个类啊。
我原来是这样配置的,但有错啊:
<bean id="baseDAO" class="com.onest.firstssh.dao.Impl.BaseDAO" abstract="true">
<property name="sessionFactory">
<ref bean="sessionFactory"/>
</property>
</bean>


代码如下:
HTML code

<?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-2.0.xsd">

<!-- 
    <bean id="dataSource"
        class="org.hibernate.dialect.MySQLDialect">
        <property name="driverClassName"
            value="com.mysql.jdbc.Driver">
        </property>
        <property name="url"
            value="jdbc:mysql://localhost:3306/test">
        </property>
        <property name="username" value="sa"></property>
        <property name="password" value="aa"></property>
    </bean>
 -->
     <!-- 数据库配置 -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <!-- 
        <property name="dataSource">
            <ref bean="dataSource"/>
        </property>
         -->
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>

    <!-- DAO实例配置 -->
    <!--
    <bean id="baseDAO" class="com.onest.firstssh.dao.Impl.BaseDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    
    <bean id="userDAO" class="com.onest.firstssh.dao.Impl.UsersDAO" parent="baseDAO">
    
    </bean>
      -->
    <bean id="userDAO" class="com.onest.firstssh.dao.Impl.UsersDAO">
        <property name="sessionFactory">
            <ref bean="sessionFactory"/>
        </property>
    </bean>
    
    <!-- Service实例BEAN管理 -->
    <bean id="userService" class="com.onest.firstssh.service.Impl.UsersService">
        <property name="userDAO">
            <ref bean="userDAO"/>
        </property>
    </bean>
    
    <!-- Action Bean实例管理 -->
    <bean id="login" class="com.onest.firstssh.actions.Login" scope="prototype">
        <property name="userService">
            <ref bean="userService"/>
        </property>
    </bean>
</beans>



------解决方案--------------------
继承接口啊,你的BaseDAO这个类里面没有抽象方法,为什么要将类定义为抽象类呢,好象没什么意义
------解决方案--------------------
解决办法: 
这只是个例子 

如果在applicationContext-service.xml中写: 
<bean id="aaa" class = "TestImplementation">
</bean>
在类中就写成: 
Test <String> aaa;