台灣印刷設計交流論壇

標題: 業務中台設計实例 - CSDN [打印本頁]

作者: admin    時間: 2022-6-15 15:37
標題: 業務中台設計实例 - CSDN
  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末節,已把扩大点注册和扩大点履行,触及到的数据模子、運行機制論述清晰了。這里再補一幅圖来個更直观感觉。




歡迎光臨 台灣印刷設計交流論壇 (https://bbs.eprintcolor.com.tw/) Powered by Discuz! X3.3