Discuz! Board

 找回密碼
 立即註冊
搜索
熱搜: 活動 交友 discuz
查看: 243|回復: 0
打印 上一主題 下一主題

業務中台設計实例 - CSDN

[複製鏈接]

2610

主題

2614

帖子

7970

積分

管理員

Rank: 9Rank: 9Rank: 9

積分
7970
跳轉到指定樓層
樓主
發表於 2022-6-15 15:37:31 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
  1. /**
  2. * 扩大点扫描、注册
  3. */
  4. public class ExtPointBeanPostProcess implements BeanPostProcessor {
  5. @Override
  6. public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
  7. return bean;
  8. }
  9. /**
  10. * 辨認扩大点实例, 并完成扩大点注册
  11. * 1.扩大点必需实現IExtensionPoint接口
  12. * 2.强迫请求扩大点接口名必需以ExtPt末端
  13. */
  14. @Override
  15. public Object台中機車借款, postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
  16. if (bean instanceof IExtensionPoint) {
  17. IExtensionPoint extPoint = (IExtensionPoint) bean;

  18. ExtensionCoordinate coordinate = parseExtensionCoordinate(bean.getClass());
  19. IExtensionPoint oldValue = ExtensionManager.getInstance()
  20. .registerExtPoint(coordinate, extPoint);
  21. if (Objects.nonNull(oldValue)) {
  22. throw new MidFrameworkException("Duplicate registration for :" + coordinate);
  23. }
  24. }
  25. return bean;
  26. }
  27. private ExtensionCoordinate parseExtensionCoordinate(Class<?> targetBean) {
  28. // 扩大点接口
  29. Optional<Class> extPointClass = checkExtensionPoint(targetBean);
  30. if (!extPointClass.isPresent()) {
  31. throw new MidFrameworkException(
  32. String.format("The name of ExtensionPoint for %s must be end with %s", targetBean, IExtensionPoint.EXT_POINT_NAMING));
  33. }
  34. // 解析@Extension注解
  35. AnnotationAttributes attrs = AnnotatedElementUtils.getMergedAnnotationAttributes(targetBean, Extension.class);
  36. String bizCode = attrs.getString("bizCode");
  37. String name = attrs.getString("name");
  38. String description = attrs.getString("description");
  39. return ExtensionCoordinate.builder()
  40. .bizCode(bizCode)
  41. .extensionPoint(extPointClass.get().getName())
  42. .name(name)
  43. .description(description)
  44. .build();
  45. }
  46. private Optional<Class> checkExtensionPoint(Class<?> targetBean) {
  47. Class[] interfaces = targetBean.getInterfaces();
  48. if (interfaces == null || interfaces.length == 0) {
  49. throw new MidFrameworkException("No extension point interface for " + targetBean);
  50. }
  51. // 扩大点接口名必需以ExtPt末端
  52. return Arrays.stream(interfaces)
  53. .filter(ext -> ext.getSimpleName().endsWith(IExtensionPoint.EXT_POINT_NAMING))
  54. .findFirst();
  55. }
  56. }
複製代碼


颠末以上步调,體系已启動完成,而且所有的扩大点也都完成为了注册。剩下的就是怎样去挪用扩大点法子了!因为统一個扩大点的所有实例多是统一個名字,好比都叫
BuyExtPointImpl,明顯不克不及經由過程Spring的依靠注入来挪用。究竟上,扩大点实例的挪用,都是同一走扩大点履行器,其内部封装了扩大点查找逻辑和履行逻辑,代码注释應當写得比力清晰了。

  1. /**
  2. * 扩大点实例履行器: 封装扩大点查找计谋及法子挪用
  3. */
  4. public class ExtensionExecutor  {
  5. private ExtensionExecutor() {
  6. throw new IllegalStateException("No instance");
  7. }
  8. /**
  9. * 有返回值
  10. *
  11. * @param targetClass 扩大点类名
  12. * @param bizId       营業身份
  13. * @param exeFunction 必要挪用的扩大点法子
  14. */
  15. public static <R, T> R execute(BizId bizId, Class<T> targetClass, Function<T, R> exeFunction) {
  16. T component = findComponent(bizId, targetClass);
  17. return exeFunction.apply(component);
  18. }
  19. /**
  20. * 無返回值
  21. *
  22. * @param targetClass 扩大点类名
  23. * @param bizId       营業身份
  24. * @param exeConsumer 必要挪用的扩大点法子
  25. */
  26. public static <T> void consume(BizId bizId, Class<T> targetClass, Consumer<T> exeConsumer) {
  27. T component = findComponent(bizId, targetClass);
  28. exeConsumer.accept(component);
  29. }
  30. /**
  31. * 切确查找扩大点: 先看是不是有营業实現的扩大点;若没有,再看是不是有平台默许实現
  32. */
  33. @SuppressWarnings("unchecked")
  34. private static <T> T findComponent(BizId bizId, Class<T> targetClass) {
  35. ExtensionCoordinate bizCoordinate = ExtensionCoordinate.of(bizId.getBizCode(), targetClass.getName());
  36. IExtensionPoint extension = ExtensionManager.getInstance().findExtPoint(bizCoordinate);
  37. if (Objects.isNull(extension)) {
  38. ExtensionCoordinate systemCoordinate = ExtensionCoordinate.of(IExtensionPoint.SYSTEM_BIZ_CODE,
  39. targetClass.getName());
  40. extension = ExtensionManager.getInstance().findExtPoint(systemCoordinate);
  41. }
  42. if (Objects.isNull(extension)) {
  43. throw new MidFrameworkException("Not found extension: " + bizCoordinate);
  44. }
  45. return (T) extension;
  46. }
  47. }
複製代碼


下面以一個单测为例演示扩大点的履行 ExtensionExecutor.execute()

  1. @Test
  2. public void testTaobaoExt() {
  3. BizId bizId = new BizId("com.taobao");
  4. int promotion = ExtensionExecutor.execute(bizId, PromotionCalcExtPoint.class,
  5. PromotionCalcExtPoint::calcPromotion);
  6. Assert.539版路單,assertEquals(promotion, 80);
  7. }
  8. @Test
  9. public void testTmallExt() {
  10. BizId bizId = new BizId("com.tmall");
  11. int promotion = ExtensionExecutor.exec君綺PTT,ute(隱形矯姿帶,bizId, PromotionCalcExtPoint.class,
  12. PromotionCalcExtPoint::calcPromotion);
  13. Assert.assertEquals(promotion, 90);
  14. }
複製代碼


上面2末節,已把扩大点注册和扩大点履行,触及到的数据模子、運行機制論述清晰了。這里再補一幅圖来個更直观感觉。
回復

使用道具 舉報

您需要登錄後才可以回帖 登錄 | 立即註冊

本版積分規則

Archiver|手機版|小黑屋|台灣印刷設計交流論壇  

歐冠杯, 歐冠杯決賽, 歐冠盃, 歐冠盃投注, 設計師, polo衫, 團體制服, 團體服, 夾克, 背心, 廚具空壓機, 滑鼠墊咽炎貼, 健身呼啦圈, 設計師, 室內裝潢, 音波拉皮, 汐止借錢, 汐止機車借款, 百家樂, 桃園室內設計, 刷卡換現, 刷卡換現金, 悠遊卡套, 未上市, 高雄合法當舖, 電動噴霧機, 懶人減肥法, 翻譯社, 未上市股票, 隆乳, 封口機淚溝, 素描畫室汐止當舖屏東借款三明治盒, 中藥, 空壓機禮品, 贈品, 借款, 借錢, 借貸, 眼睛保健食品推薦, 灰指甲治療專用藥, 水果茶,

GMT+8, 2024-4-29 01:47 , Processed in 0.094473 second(s), 5 queries , File On.

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回復 返回頂部 返回列表