如何在供应商内部注入服务。储存库为空

时间:2019-07-08 13:39:57

标签: spring spring-boot

我使用供应商创建了工厂模式。没关系,而且效果很好。正确的服务的路径是可以的,但是在将要计算的服务内部,存储库为空。似乎没有在自动接线。

我尝试使用@Service和@Component自动装配和注释类,但仍然无法正常工作。

@Service
public class Service {

    public responseDTO calculate(Contract contract, Request request) {

        Supplier<TypeFactory> type = TypeFactory::new;

        //HOW TO AUTOWIRE THIS?
        return type.get().getCalculationMethod(contract.getId()).calculate(request);

    }

}


@Service
public class TypeFactory {

    final static Map<Integer, Supplier<Calculator>> calculationTypeMap = new HashMap<>();

    static {
        calculationTypeMap.put(1, ContractOneType::new);
        calculationTypeMap.put(2, ContractTwoType::new);
    }

    public Calculator getCalculationMethod(Integer type) {
        Supplier<Calculator> method = calculationTypeMap.get(type);
        if (method != null) {
            return method.get();
        }
        throw new IllegalStateException("Type not found");
    }

}
@Service
public interface Calculator {

    ResponseDTO calculate(Request Request);

}
@Service
public class ContractOneType implements Calculator {

    @Autowired
    private ContractRepository contractRepository;

    public ResponseDTO calculate(Request request) {

        ResponseDTO responseDTO = new ResponseDTO();

        Contract contract = contractRepository.findById(request.getId()).orElseThrow(() -> new NotFoundException("id not found"));

        //some calculations here with the contract

        return responseDTO;

    }

}

contractRepository为null,未自动接线。一定是。

我的代码中的错误消息:

  

2019-07-08 10:19:33.078错误   [-,21fa294c89e1e207,21fa294c89e1e207,false] 19477 ---   [nio-8080-exec-1] c.l.d.t.s.h.GeneralExceptionHandler:   msg =“ Exception”,stacktrace =“ java.lang.NullPointerException

1 个答案:

答案 0 :(得分:0)

我找到了一个更改供应商的解决方案,该解决方案正在创建新实例,我只是为实现创建实例,并为它们设置@Autowired。

这就是为什么我的存储库没有@Autowired的原因。

这是我的代码:

@Service
public class TypeFactory {

    final static Map<Integer, Supplier<Calculator>> calculationTypeMap = new HashMap<>();

    @Autowired
    ContractOneType contractOneType;

    @Autowired
    ContractTwoType contractTwoType;

    static {
        calculationTypeMap.put(1, contractOneType);
        calculationTypeMap.put(2, contractTwoType);
    }

    public Calculator getCalculationMethod(Integer type) {
        Supplier<Calculator> method = calculationTypeMap.get(type);
        if (method != null) {
            return method.get();
        }
        throw new IllegalStateException("Type not found");
    }

}
@Component
public class ContractOneType implements Calculator {

    @Autowired
    private ContractRepository contractRepository;

    public ResponseDTO calculate(Request request) {

        ResponseDTO responseDTO = new ResponseDTO();

        Contract contract = contractRepository.findById(request.getId()).orElseThrow(() -> new NotFoundException("id not found"));

        //some calculations here with the contract

        return responseDTO;

    }

}
public interface Calculator {
    ResponseDTO calculate(Request Request);

}