我有一个BaseDAO和一些子类,例如:UserDaoImplOrderDaoImpl,现在在BaseService

public abstract class Baservice{

    @Resource
    private BaseDao basedao;
    //something crud operate.....
}

@Service
public UserService extends BaseService{
   //....
}

如何配置Spring,以便我可以将真实的子类传递给basedao

我遇到一个例外
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userService': Injection of resource fields failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.cloudking.trafficmonitor.BaseDAO] is defined: expected single matching bean but found 3: [roleDAO, userDAO

最佳答案

看来您有3个扩展BaseDAO的类,因此它们都是BaseDao(IS A)

您需要使用name@Resource属性

例如:

@Component
public RoleDao extends BaseDao{/* some code*/}


@Component
public UserDao extends BaseDao{/* some code*/}

现在,如果您想注入UserDao,则
@Resource(name="userDao")
BaseDao baseDao

10-05 23:21