工厂方法设计模式
程序员文章站
2024-01-05 23:09:40
...
SpringUtils
TemplateFactory
@Component
public class TemplateFactory {
@Autowired
private SpringUtils springUtils;
public AbstractPayCallbackTemplate getPayCallbackTemplate(String templateId) {
AbstractPayCallbackTemplate payCallbackTemplate = (AbstractPayCallbackTemplate) springUtils.getBean(templateId);
return payCallbackTemplate;
}
}
FactoryTempleteApp
@SpringBootApplication
@EnableAsync
@RestController
@Slf4j
public class FactoryTempleteApp {
public static void main(String[] args) {
SpringApplication.run(FactoryTempleteApp.class, args);
}
@Autowired
private TemplateFactory templateFactory;
@RequestMapping(value = "/factory/{id}", method = RequestMethod.GET)
public String factory(@PathVariable("id") String id) {
log.info("beanid:{}", id);
//id表示beanID
return templateFactory.getPayCallbackTemplate(id).asycCallback();
}
}
测试
http://localhost:8088/factory/aliPayCallbackTemplate
www.mayikt.com