繁体   English   中英

与实现接口相比,在抽象类的具体类中自动装配

[英]Autowiring in concrete classes of an abstract class than implements an interface

我正在使用Spring Boot并且具有以下任务模型

public class Task {
    private String name;
    private TaskType type; // ManualTask, AutomatedTask
    private boolean completed;
    //....other fields

    //getters and setters
}

控制器

@Controller
@RequestMapping("/api/task")
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/{taskId}/handle")
    public String handle(Model model, @PathVariable("taskId") Long taskId) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        try {
            Task task = taskService.handle(taskId);
            model.addAttribute("task", task);
        } catch (Exception e) {
            return "errorpage";
        }
        return "successpage";
    }
}

我有一个界面

public interface TaskService {

    Task findById(Long taskId);

    Task handleTask(Long taskId) throws ClassNotFoundException, InstantiationException, IllegalAccessException;

}

抽象类实现接口:

@Service
public abstract class TaskServiceImpl implements TaskService {

    @Autowired
    private TaskRepository taskRepository;

    private static final String PATH_OF_CLASS = "com.task.service.impl";

    protected abstract Task doTypeSpecificTask(Long taskId);

    @Override
    public Task findById(Long taskId) {
        return taskRepository.findById(taskId).get();
    }

    @Override
    public Task handleTask(Long taskId) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        Task task = findById(taskId);
        TaskServiceImpl service = getHandlerService(task);
        return service.doTypeSpecificTask(taskId);
    }

    private TaskServiceImpl getHandlerService(Task task) throws ClassNotFoundException, InstantiationException, IllegalAccessException {
        String serviceClassName = PATH_OF_CLASS.concat(".").concat(task.getTaskType().getName()).concat("Service");
        Class<?> serviceClass = Class.forName(serviceClassName);
        if (!TaskServiceImpl.class.isAssignableFrom(serviceClass)) {
            throw new RuntimeException("Service class " + serviceClassName + " did not implements " + TaskServiceImpl.class.getName());
        }
        Object serviceObject = serviceClass.newInstance();
        TaskServiceImpl service = (TaskServiceImpl) serviceObject;
        return service;
    }


}

以及扩展抽象类的具体服务

@Service
@Primary
public class ManualTaskService extends TaskServiceImpl {

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private ManualTaskHandlerService manualTaskHandlerService;

    @Override
    protected Task doTypeSpecificTask(Long taskId) {
        Task task = findById(taskId);
        manualTaskHandlerService.handleManualTask(task);
        task.setCompleted(true);
        return taskRepository.save(task);
    }
}

@Service
public class AutomatedTaskService extends TaskServiceImpl {

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private AutomatedTaskHandlerService automatedTaskHandlerService;

    @Override
    protected Task doTypeSpecificTask(Long taskId) {
        Task task = findById(taskId);
        automatedTaskHandlerService.handleAutomatedTask(task);
        task.setCompleted(true);
        return taskRepository.save(task);
    }
}

public interface TaskRepository extends JpaRepository<Task, Long> {

}

根据运行时任务的类型动态选择ManualTaskServiceAutomatedTaskService

现在,如果没有@Primary@Primary出现以下错误:

Field taskService in com.test.controller.TaskController required a single bean, but 2 were found:
- manualTaskService
- automatedTaskService

Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

@Primary设置@Primary时, doTypeSpecificTask中的doTypeSpecificTask可以工作,但在AutomatedTaskService中,它会由于automaticTaskHandlerService.handleAutomatedTask automatedTaskHandlerService.handleAutomatedTask(task)而失败。 从AutomatedTaskService调用taskRepository也会失败。

我尝试使用@Qualifier以及将抽象类中的所有@Autowired定义为protected,但无济于事。 我究竟做错了什么?

每个预选赛应该使用不同的名称:

@Autowired
@Qualifier("manualTaskService")
private TaskServiceImpl manualTaskService;

@Autowired
@Qualifier("automatedTaskService")
private TaskServiceImpl automatedTaskService;

服务中定义了以下内容:

@Service("manualTaskService")
public class ManualTaskService extends TaskServiceImpl {

@Service("automatedTaskService")
public class AutomatedTaskService extends TaskServiceImpl {

我通过使用链接中提到的工厂模式解决了这个问题(感谢@ user7294900提供了链接)

我完全删除了抽象类TaskServiceImpl 相反,我创建了两个新接口ManualTaskServiceAutomatedTaskService它们都扩展了TaskService接口

public interface ManualTaskService extends TaskService {
}

public interface AutomatedTaskService extends TaskService {     
}

然后我创建了一个TaskServiceFactory

@Component
public class TaskServiceFactory {

    @Autowired
    private ManualTaskService manualTaskService;

    @Autowired
    private AutomatedTaskService automatedTaskService;

    public TaskService getService(TaskType type) throws Exception {
        switch (type) {
        case MANUAL_TASK:
            return manualTaskService;
        case AUTOMATED_TASK:
            return automatedTaskService;
        default:
            throw new Exception("Unrecognized task type");
        }
    }
}

接下来,我为ManualTaskServiceAutomatedTaskService创建了实现

@Service
public class ManualTaskServiceImpl implements ManualTaskService {

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private ManualTaskHandlerService manualTaskHandlerService;

    @Override
    public Task findById(Long taskId) {
        return taskRepository.findById(taskId).get();
    }

    @Override
    public Task handleTask(Long taskId) throws Exception {
        Task task = findById(taskId);
        manualTaskHandlerService.handleManualTask(task);
        task.setCompleted(true);
        return taskRepository.save(task);
    }
}


@Service
public class AutomatedTaskServiceImpl implements AutomatedTaskService {

    @Autowired
    private TaskRepository taskRepository;

    @Autowired
    private AutomatedTaskHandlerService automatedTaskHandlerService;

    @Override
    public Task findById(Long taskId) {
        return taskRepository.findById(taskId).get();
    }

    @Override
    public Task handleTask(Long taskId) throws Exception {
        Task task = findById(taskId);
        automatedTaskHandlerService.handleAutomatedTask(task);
        task.setCompleted(true);
        return taskRepository.save(task);
    }

}

最后,我更新了控制器以从用户那里获取任务类型,然后使用TaskServiceFactory根据类型获取正确的服务实例。

@Controller
@RequestMapping("/api/task")
public class TaskController {

    @Autowired
    private TaskServiceFactory taskServiceFactory;

    @PostMapping("/{taskId}/handle")
    public String handle(Model model, @PathVariable("taskId") Long taskId, HttpServletRequest request) throws Exception {
        try {
            TaskType type = TaskType.valueOf(request.getParameter("type"));
            Task task = taskServiceFactory.getService(type).handleTask(taskId, request);
            model.addAttribute("task", task);
        } catch (Exception e) {
            return "errorpage";
        }
        return "successpage";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM