如何在命令设计模式中注入存储库?
How to inject a repository in command design pattern?
在少数 classes 中,我有类似的 else if 语句,我想使用命令模式替换这些语句。这个想法是将 else if 主体外部化,并根据调用它的服务使用其存储库。问题是我似乎无法找到将存储库注入我的 class 的方法。我会在示例中更好地解释:
应该使用命令模式的部分代码:
private HashMap<String, BnResponseHandler> commandMap = new HashMap<>();
for (CustomerDelivery customerDelivery : list) {
commandMap.put("true", new SuccessfulResponse(customerDelivery));
commandMap.put("1", new ResponseCodeOne(customerDelivery));
commandMap.put("2", new ResponseCodeTwo(customerDelivery));
commandMap.put("3", new ResponseCodeThree(customerDelivery));
BnResponseHandler bnResponseHandler = new DefaultHandler(customerDelivery);
if(response.isSuccessful()){
commandMap.get(String.valueOf(response.isSuccessful())).handle();
} else if (response.getResponseCode() != null
&& !response.getResponseCode().equals("")){
commandMap.get(response.getResponseCode()).handle();
} else {
bnResponseHandler.handle();
}
}
界面:
public interface BnResponseHandler {
void handle();
}
和接口实现:
@Component
public class ResponseCodeOne implements BnResponseHandler {
@Autowired
private FabricationRepository fabricationRepo;
private Fabrication fabrication;
@Autowired
private MovementsRepository movementsRepository;
private StockMovements stockMovements;
@Autowired
private CustomerDeliveryRepository customerDeliveryRepo;
private CustomerDelivery customerDelivery;
public ResponseCodeOne() {
}
public ResponseCodeOne(Fabrication fabrication) {
this.fabrication = fabrication;
}
public ResponseCodeOne(StockMovements stockMovements) {
this.stockMovements = stockMovements;
}
public ResponseCodeOne(CustomerDelivery customerDelivery) {
this.customerDelivery = customerDelivery;
}
@Override
public void handle() {
if (fabrication != null) {
fabrication.setFG_ETAT("D");
fabrication.setLB_ETAT("");
fabricationRepo.save(fabrication);
} else if (stockMovements != null) {
stockMovements.setFG_ETAT("D");
stockMovements.setLB_ETAT("");
movementsRepository.save(stockMovements);
} else if (customerDelivery != null) {
customerDelivery.setFG_ETAT("D");
customerDelivery.setLB_ETAT("");
customerDeliveryRepo.save(customerDelivery);
}
}
}
所以大局是为我可以获得的每个可能的响应代码(在本例中为 1)设置 ResponseCode classes,并在这些 classes 中执行一些业务逻辑,这取决于存储库 bean。它还应该是灵活的,以便我可以将 ResponseCodeOne class 用于多种不同的情况(意味着制造、stockMovements 和 customerDelivery)。我遇到的问题是我的存储库没有以这种方式注入,并且我不断收到空指针异常。有没有一种方法可以干净地注入我的存储库 bean?
我看到的主要问题是您正在使用 new
关键字创建自己的响应代码 classes 实例,这意味着 Spring 不会自动装配它们。为了让它们成功自动装配,您需要将它们的实例注入到您的 for 循环所在的 class 中。但是,这会引入一个次要问题,即您无法再从 for 循环中向响应代码实例的构造函数提供对象。要解决此问题,您可以将 Fabrication
/StockMovements
/CustomerDelivery
实例作为参数传递给 handle()
.
在少数 classes 中,我有类似的 else if 语句,我想使用命令模式替换这些语句。这个想法是将 else if 主体外部化,并根据调用它的服务使用其存储库。问题是我似乎无法找到将存储库注入我的 class 的方法。我会在示例中更好地解释:
应该使用命令模式的部分代码:
private HashMap<String, BnResponseHandler> commandMap = new HashMap<>();
for (CustomerDelivery customerDelivery : list) {
commandMap.put("true", new SuccessfulResponse(customerDelivery));
commandMap.put("1", new ResponseCodeOne(customerDelivery));
commandMap.put("2", new ResponseCodeTwo(customerDelivery));
commandMap.put("3", new ResponseCodeThree(customerDelivery));
BnResponseHandler bnResponseHandler = new DefaultHandler(customerDelivery);
if(response.isSuccessful()){
commandMap.get(String.valueOf(response.isSuccessful())).handle();
} else if (response.getResponseCode() != null
&& !response.getResponseCode().equals("")){
commandMap.get(response.getResponseCode()).handle();
} else {
bnResponseHandler.handle();
}
}
界面:
public interface BnResponseHandler {
void handle();
}
和接口实现:
@Component
public class ResponseCodeOne implements BnResponseHandler {
@Autowired
private FabricationRepository fabricationRepo;
private Fabrication fabrication;
@Autowired
private MovementsRepository movementsRepository;
private StockMovements stockMovements;
@Autowired
private CustomerDeliveryRepository customerDeliveryRepo;
private CustomerDelivery customerDelivery;
public ResponseCodeOne() {
}
public ResponseCodeOne(Fabrication fabrication) {
this.fabrication = fabrication;
}
public ResponseCodeOne(StockMovements stockMovements) {
this.stockMovements = stockMovements;
}
public ResponseCodeOne(CustomerDelivery customerDelivery) {
this.customerDelivery = customerDelivery;
}
@Override
public void handle() {
if (fabrication != null) {
fabrication.setFG_ETAT("D");
fabrication.setLB_ETAT("");
fabricationRepo.save(fabrication);
} else if (stockMovements != null) {
stockMovements.setFG_ETAT("D");
stockMovements.setLB_ETAT("");
movementsRepository.save(stockMovements);
} else if (customerDelivery != null) {
customerDelivery.setFG_ETAT("D");
customerDelivery.setLB_ETAT("");
customerDeliveryRepo.save(customerDelivery);
}
}
}
所以大局是为我可以获得的每个可能的响应代码(在本例中为 1)设置 ResponseCode classes,并在这些 classes 中执行一些业务逻辑,这取决于存储库 bean。它还应该是灵活的,以便我可以将 ResponseCodeOne class 用于多种不同的情况(意味着制造、stockMovements 和 customerDelivery)。我遇到的问题是我的存储库没有以这种方式注入,并且我不断收到空指针异常。有没有一种方法可以干净地注入我的存储库 bean?
我看到的主要问题是您正在使用 new
关键字创建自己的响应代码 classes 实例,这意味着 Spring 不会自动装配它们。为了让它们成功自动装配,您需要将它们的实例注入到您的 for 循环所在的 class 中。但是,这会引入一个次要问题,即您无法再从 for 循环中向响应代码实例的构造函数提供对象。要解决此问题,您可以将 Fabrication
/StockMovements
/CustomerDelivery
实例作为参数传递给 handle()
.