以下是DAO接口public interface IAuthenticateDAO {
    public boolean IsAvailableInTheAccount(AuthenticateVO authenticateVO);    public boolean IsAlreadyCertified(AuthenticateVO authenticateVO);    public void addAuthenticationInfoAndGiftLine(AuthenticateVO authenticateVO);
    
}下面两个是实现类public class PhoneAuthenticateDAOImpl implements IAuthenticateDAO {
    
    public boolean IsAvailableInTheAccount(AuthenticateVO authenticateVO) {
        return false;
    }    public boolean IsAlreadyCertified(AuthenticateVO authenticateVO) {
        return false;
    }    public void addAuthenticationInfoAndGiftLine(AuthenticateVO authenticateVO) {
    }
}
public class EmailAuthenticateDAOImpl implements IAuthenticateDAO {    @Override
    public boolean IsAvailableInTheAccount(AuthenticateVO authenticateVO) {
        return false;
    }    @Override
    public boolean IsAlreadyCertified(AuthenticateVO authenticateVO) {
        return false;
    }    @Override
    public void addAuthenticationInfoAndGiftLine(AuthenticateVO authenticateVO) {
    }
}
这样写的话 在Service层,使用注解的方式应该怎么配? 是不是这样:@Service("authenticateService")
public class AuthenticateServiceImpl implements IAuthenticateService {
    @Inject
    private IAuthenticateDAO phoneAuthenticateDAO;
    @Inject
    private IAuthenticateDAO emailAuthenticateDAO;
}

解决方案 »

  1.   

    首先我想你注入的两个bean也因该交给spring管理。这样才能注入成功。
      

  2.   

    @Repository("emailAuthenticateDAO")
    public class EmailAuthenticateDAOImpl implements IAuthenticateDAO {}
    这个考虑了。。 问题是我不知道是否会成功。
      

  3.   

    这样能够注入成功,如果是注入方式是byName。但是问题是这样就失去了多态的意义。我们要多态是想由程序在运行的时候时候决定注入哪个实现类,但是用spring的注入方式来实现多态貌似实现不了。比如:@Repository
    public class daoImpl implements dao{
    @Resource
    Idatabase idb;
    void save(T entity){ idb.executeSQL(entity);//实现类 SaveImpl中重写的方法 }
    void update(T entity){ idb.executeSQL(entity);//实现类 UpdateImpl中重写的方法 }
    void remove(T entity){ idb.executeSQL(entity);//实现类 RemoveImpl中重写的方法 }
    }在以上代码我们希望由容器判断该注入什么类,但是实际上貌似不行,会抛出异常。
      

  4.   

    你想想如果是你  没有指定名字 你能确定
     @Inject
     private IAuthenticateDAO phoneAuthenticateDAO;
     @Inject
     private IAuthenticateDAO emailAuthenticateDAO;
    这两个具体是IAuthenticateDAO的哪个实现类吗