日期:2014-05-17  浏览次数:20973 次

spring注解注入泛型子类
本帖最后由 fank243 于 2013-10-17 23:37:15 编辑
最近在学spring的注解,但是现在遇到难题了,望各位帮下小弟的忙?
以下是项目的工程图

dao和service都使用了泛型设计
泛型dao接口
public interface BaseDao<T, PK extends Serializable> {

泛型dao实现类
public class BaseDaoImpl<T, PK extends Serializable> implements BaseDao<T, PK> {

学员接口
public interface StudentDao extends BaseDao<Student, Integer> {

学员实现类
@Repository
public class StudentDaoImpl extends BaseDaoImpl<Student, Integer> implements StudentDao {

使用junit进行测试的时候会报下面的异常。
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [cn.ssh.annotation.dao.BaseDao] is defined: expected single matching bean but found 4: baseDaoImpl,studentDaoImpl,teacherDaoImpl,studentServiceImpl
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
StudentDao studentDaoImpl = (StudentDaoImpl) context.getBean("studentDaoImpl")


不知道为什么会这样,难道spring不支持泛型的注解吗?
还有想问下,ssh整合的话,有哪些注解可以用,每个框架都要相应的jar包吗?
spring如何对事物进行注解呢?
或者能够提供相应的文档或者链接地址.
小弟将万分感谢!!!
------解决方案--------------------
spring 支持范型。
建议你不要继承BaseDAO,将其作为StudentDAOImp的成员属性,即添加
private BaseDAO baseDAO;
public void setBaseDAO(BaseDAO arg0){
    this.baseDAO = arg0;
}
public BaseDAO getBaseDAO(){
    return this.baseDAO
}

在 SSH 中所有实现类都可以注解。
spring 用@Transaction 来注解,让其支持事务。
文档什么的建议去官网看 http://spring.io
------解决方案--------------------
在 SSH 中所有实现类都可以注解。
 spring 用@Transaction 来注解,让其支持事务。
 文档什么的建议去官网看 http://spring.io