Java 如何使用spring boot本机支持更好地实现factory模式?

Java 如何使用spring boot本机支持更好地实现factory模式?,java,spring-boot,factory-pattern,Java,Spring Boot,Factory Pattern,在Spring boot中实现工厂模式的最佳方法 我有一个接口和它的多个实现。在请求期间,我需要根据输入字符串返回bean 我有多种方法可以做到这一点。。但最好的办法是什么 interface vehicle { void drive(); string getVehicleName(); } @Component public class Car implements vehicle { private static string prop = "car"; @Override

在Spring boot中实现工厂模式的最佳方法

我有一个接口和它的多个实现。在请求期间,我需要根据输入字符串返回bean

我有多种方法可以做到这一点。。但最好的办法是什么

interface vehicle {
void drive();
string getVehicleName();
}

@Component
public class Car implements vehicle {
  private static string prop = "car";
  @Override
  string getVehicleName() { return prop;}

  @Override
  void drive() {}
}

@Component
public class Bike implements vehicle {
  private static string prop = "bike";

  @Override
  string getVehicleName() { return prop;}

  @Override
  void drive() {}
}

@Service
public class VehicleFactory {
    @Autowired
    private List<vehicle> vehicles;

    private static final HashMap<String, vehicle> requestVehicleMap = new HashMap<>();

    @PostConstruct
    public void initVehicleFactory() {
        for(vehicle vehicle : vehicles) {
            requestVehicleMap.put(vehicle.getVehicleName(), request);
        }
    }

    public static vehicle getVehicleImpl(String vehicleName) {
        return requestVehicleMap.get(vehicleName);
    }
}
接口车辆{
无效驱动();
字符串getVehicleName();
}
@组成部分
公车{
私有静态字符串prop=“car”;
@凌驾
字符串getVehicleName(){return prop;}
@凌驾
无效驱动器(){}
}
@组成部分
公营自行车车{
私有静态字符串prop=“bike”;
@凌驾
字符串getVehicleName(){return prop;}
@凌驾
无效驱动器(){}
}
@服务
公共级车辆制造厂{
@自动连线
私家车名单;
私有静态最终HashMap requestVehicleMap=新HashMap();
@施工后
公共车辆制造厂(){
用于(车辆:车辆){
requestVehicleMap.put(vehicle.getVehicleName(),request);
}
}
公共静态车辆getVehicleImpl(字符串vehicleName){
返回请求VehicleMap.get(vehicleName);
}
}
这确实给了我正确的分类。 还有一个“限定符”可以用作


但是有更好的方法吗?

接口和它的实现都很好,我只想单独更改Factory类,因为您已经得到了实现列表,那么为什么还要在映射中对其进行初始化呢

我也会评论代码中的建议

汽车厂
@服务
公共级车辆制造厂{
@自动连线
私家车名单;
公共车辆getVehicleImpl(字符串vehicleName){//您已经声明为@Service,那么为什么要使用静态
返回车辆。流()
.filter(vehicle->vehicle.getVehicleName().equalsIgnoreCase(vehicleName))//这将从其他人那里筛选所需的Impl
.findFirst()
.orelsetrow(()->new RuntimeException(String.format(“invlaed Vehicle Name-%s”,vehicleName));//如果找不到Impl,它应该以其他方式抛出错误或句柄
}
}

所以给它一个try

接口,它的实现很好,我只想单独更改Factory类,因为您已经得到了实现列表,那么为什么还要在映射中初始化它呢

我也会评论代码中的建议

汽车厂
@服务
公共级车辆制造厂{
@自动连线
私家车名单;
公共车辆getVehicleImpl(字符串vehicleName){//您已经声明为@Service,那么为什么要使用静态
返回车辆。流()
.filter(vehicle->vehicle.getVehicleName().equalsIgnoreCase(vehicleName))//这将从其他人那里筛选所需的Impl
.findFirst()
.orelsetrow(()->new RuntimeException(String.format(“invlaed Vehicle Name-%s”,vehicleName));//如果找不到Impl,它应该以其他方式抛出错误或句柄
}
}

所以试试看

两个问题:1。私家车在何处登记;;来自2.车辆是弹簧组件、DTO还是实体?我同意Simon的观点@哈希尼,你的问题是“有更好的方法吗?”,在什么意义上?请让问题更具体,以便其他人可以帮助您。车辆列表使用弹簧自动连接。。。因此,它将拥有所有的实现。另外,当我说更好的方法时,我的意思是问SpringBoot是否提供了更好的方法。私家车在何处登记;;来自2.车辆是弹簧组件、DTO还是实体?我同意Simon的观点@哈希尼,你的问题是“有更好的方法吗?”,在什么意义上?请让问题更具体,以便其他人可以帮助您。车辆列表使用弹簧自动连接。。。因此,它将拥有所有的实现。另外,当我说更好的方法时,我的意思是问spring boot是否提供了更好的方法……是的,这也可以。。但我的观点是要有更干净的实现,并使用spring的任何内置支持。。但我的观点是要有更干净的实现,并使用spring的任何内置支持。