Spring依赖注入和泛型类

时间:2013-08-17 11:17:33

标签: spring generics dependency-injection

抱歉我的英文。我不能在春天为泛型类做依赖注入。通用类:

abstract class BaseBO<Id, Entity, DAOClass extends DAO<Id, Entity>> implements BO<Id, Entity, DAOClass> {
    DAOClass dao;

    public DAOClass getDAO() {
        return dao;
    }
    //...
}

使用泛型类:

public class TaskBO extends BaseBO<Long, Task, TaskDAO> implements BO<Long, Task, TaskDAO> {

}

我想在属性TaskBO的类TaskDAO中进行依赖注入。 但我只能通过bean TaskDAO的接口DAO设置依赖TaskBO

<bean id="TaskBO" class="com.otv.model.bo.TaskBO">
        <property name="DAO" ref="TaskDAO" />
</bean>

<bean id="TaskDAO" class="com.otv.model.dao.TaskDAO">
        <property name="sessionFactory" ref="SessionFactory" />
</bean>

如何通过类TaskDAO设置依赖注入?

1 个答案:

答案 0 :(得分:0)

如果你有一个堆栈跟踪,请附加错误。

在对属性访问者使用一致的案例之前:

abstract class BaseBO<Id, Entity, DAOClass extends DAO<Id, Entity>> implements BO<Id, Entity, DAOClass> {
    DAOClass dao;

    public DAOClass getDao() {
        return dao;
    }
    //...
}

很可能你需要一个BaseBO.dao属性的setter

public void setDao(DAOClass dao) {
  this.dao = dao;
}

public class TaskBO extends BaseBO<Long, Task, TaskDAO> implements BO<Long, Task, TaskDAO> {

    public void setDao(TaskDAO dao) {
      super.dao = dao;
    }

}