【问题标题】:Factory design patter Spring Boot double bean工厂设计模式 Spring Boot 双 bean
【发布时间】:2021-02-11 00:31:52
【问题描述】:
@Component
public abstract class CommandBase {

    @Autowired
    WebServiceProxy nbiService;

    @Autowired
    OperationCacheRepository cacheRepository;

    public CommandBase(
            WebServiceProxy nbiService,
            OperationCacheRepository cacheRepository) {

        this.nbiService = nbiService;
        this.cacheRepository = cacheRepository;
    }

    public abstract void executeSPV(SpeedTestDTO stDTO) throws NBIException;

    public abstract long executeGPV(long guid, OperationCache operationCache) throws NBIException;
@Slf4j
public class DownloadDiagnosticsCommand extends CommandBase {

    public DownloadDiagnosticsCommand(WebServiceProxy nbiService, OperationCacheRepository cacheRepository) {
        super(nbiService, cacheRepository);
    }

    @Override
    public void executeSPV(SpeedTestDTO stDTO) throws NBIException {
        // some executable code
    }

    @Override
    public long executeGPV(long guid, OperationCache operationCache) throws NBIException {
        // some executable code
    }
}

@Slf4j
public class UploadDiagnosticsCommand extends CommandBase {

    public UploadDiagnosticsCommand(WebServiceProxy nbiService, OperationCacheRepository cacheRepository) {
        super(nbiService, cacheRepository);
    }

    @Override
    public void executeSPV(SpeedTestDTO stDTO) throws NBIException {
        // some executable code
    }

    @Override
    public long executeGPV(long guid, OperationCache operationCache) throws NBIException {
        //some executable code
    }
}

@Component
public class RFACommandFactory {

    @Autowired
    WebServiceProxy nbiServiceProxy;

    @Autowired
    OperationCacheRepository cacheRepository;

    public final CommandBase createCommand(final String measureType) {
        if ("download".equalsIgnoreCase(measureType)) {
            return new DownloadDiagnosticsCommand(nbiServiceProxy, cacheRepository);
        } else if ("upload".equalsIgnoreCase(measureType)) {
            return new UploadDiagnosticsCommand(nbiServiceProxy, cacheRepository);
        }
        return null;
    }
}

从抽象类调用方法executeSPV

@RestController
@RequestMapping("/rfa/speedtest/v1")
@Slf4j
public class Controller {

    @Autowired
    CommandBase command;

    @Autowired
    RFACommandFactory rfaCommandFactory;

    @PostMapping(value = "{id}", consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    private ResponseEntity<String> post(
            @PathVariable String assetId,
            @RequestBody Payload payload) {

        log.info("Received new payload:{}", payload);

        command = rfaCommandFactory.createCommand(speedTestDTO.getType());

        try {
            command.executeSPV(speedTestDTO);
        } catch (NBIException e) {
            log.info("NBIException", e);
            return new ResponseEntity(payload, HttpStatus.BAD_REQUEST);
        }
        return new ResponseEntity(payload, HttpStatus.CREATED);
    }
}

如果我从上传和下载类中删除 @Componet 我收到错误我需要为抽象类 CommndBase 添加 Bean

如果我在上传和下载类上使用@Compoment,我收到的双 Bean 被使用... .Controller 中的字段命令需要一个 bean,但找到了 2 个:

【问题讨论】:

  • 只是好奇,用@Component 注释abstract 类是否有意义(至少对您而言)?
  • 刚刚删除它,只有工厂类有 @Component 并且它可以工作,猜它没有

标签: java spring-boot


【解决方案1】:

你不应该将@Component 用于抽象类,因为 Spring 上下文将无法初始化该 bean。那么你应该删除它。

另一件事是你想在这里实现工厂模式的方式——我推荐你这里描述的方式:https://stackoverflow.com/a/39361500/14056755,重构版本https://stackoverflow.com/a/55060326/14056755

【讨论】:

  • Tnx 寻求帮助
猜你喜欢
  • 2021-04-26
  • 2019-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-20
  • 2010-09-06
  • 1970-01-01
相关资源
最近更新 更多