publicinterfaceIModel{StringacquireModelName();voidsayHello();}@ComponentpublicclassGeminiModelimplementsIModel{@OverridepublicStringacquireModelName(){returnModelEnum.GEMINI.name();}@OverridepublicvoidsayHello(){System.out.println("Hello, I am GeminiModel");}}@ComponentpublicclassGptModelimplementsIModel{@OverridepublicStringacquireModelName(){returnModelEnum.GPT.name();}@OverridepublicvoidsayHello(){System.out.println("Hello, I am GptModel");}}@ComponentpublicclassModelManager{@AutowiredprivateList<IModel> models;publicIModelacquireModel(String modelName){if(CollectionUtils.isNotEmpty(models)){return models.stream().filter(model ->StringUtils.equals(modelName, model.acquireModelName())).findFirst().orElseThrow(()->newRuntimeException("no model"));}else{thrownewRuntimeException("no model");}}}publicenumModelEnum{GPT,GEMINI;}@ComponentScanpublicclassApplication{publicstaticvoidmain(String[] args){AnnotationConfigApplicationContext applicationContext =newAnnotationConfigApplicationContext(Application.class);ModelManager modelManager = applicationContext.getBean(ModelManager.class);IModel model = modelManager.acquireModel(ModelEnum.GPT.name());
model.sayHello();}}