Java中如何实现一个接口拥有多个实现类

1、接口

public interface OrdinaryService {
    JsonData queryByKeywordPagination(Integer page, Integer counts,Object parameter[]);
}

2、实现类

@Service("maintenanceService")
public class MaintenanceServiceImpl implements MaintenanceService, OrdinaryService {
    @Override
    public JsonData queryByKeywordPagination(Integer page, Integer counts, Object[] parameter) {
        return null;
    }
}
@Service("inspectionService")
public class InspectionServiceImpl implements InspectionService,OrdinaryService  {
    @Override
    public JsonData queryByKeywordPagination(Integer page, Integer counts, Object[] parameter) {
        return null;
    }
}

3、根据不同的条件调用不同的实现

方法一

@Resource(name="maintenanceService")
OrdinaryService maintenanceService;
    
@Resource(name="inspectionService")
OrdinaryService inspectionService;

方法二

@Autowired
@Qualifier("maintenanceService")
OrdinaryService maintenanceService;
 
@Autowired
@Qualifier("inspectionService")
OrdinaryService inspectionService;

 

posted on 2021-08-10 11:06  Neonuu  阅读(1396)  评论(0编辑  收藏  举报