欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

工厂方法设计模式

程序员文章站 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

上一篇:

下一篇: