如何在 Spring Boot 应用程序中声明 Activiti 自定义 FormType



我有一个使用 Activiti 框架运行工作流的 Spring Boot 应用程序。我已经到了需要创建一个新的 FormType 的地步,以便我可以支持工作流中比字符串/长整型等更复杂的字段。

我一直关注的文章是 http://www.jorambarrez.be/blog/2013/03/13/creating-a-new-form-property-in-activiti/

我的表单类型声明为

public class PurchaseOrderFormType extends AbstractFormType {
    private static final String FORM_TYPE = "purchaseorder";
    private String name;
    private String supplier;
    private String shippingAddress;
    private String billingAddress;
    private String issuedByAddress;
    private List<PurchaseOrder> purchaseOrders;
    public String getSupplier() {
        return supplier;
    }
    public void setSupplier(String supplier) {
        this.supplier = supplier;
    }
    public String getShippingAddress() {
        return shippingAddress;
    }
    public void setShippingAddress(String shippingAddress) {
        this.shippingAddress = shippingAddress;
    }
    public String getBillingAddress() {
        return billingAddress;
    }
    public void setBillingAddress(String billingAddress) {
        this.billingAddress = billingAddress;
    }
    public String getIssuedByAddress() {
        return issuedByAddress;
    }
    public void setIssuedByAddress(String issuedByAddress) {
        this.issuedByAddress = issuedByAddress;
    }
    public List<PurchaseOrder> getPurchaseOrders() {
        return purchaseOrders;
    }
    public void setPurchaseOrders(List<PurchaseOrder> purchaseOrders) {
        this.purchaseOrders = purchaseOrders;
    }
    @Override
    public String getName() {
        return FORM_TYPE;
    }
    @Override
    public Object convertFormValueToModelValue(String propertyValue) {
        PurchaseOrderFormType purchaseOrderFormType = new PurchaseOrderFormType();
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            purchaseOrderFormType = objectMapper.readValue(propertyValue,
                PurchaseOrderFormType.class);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return purchaseOrderFormType;
    }
    @Override
    public String convertModelValueToFormValue(Object modelValue) {
        ObjectMapper objectMapper = new ObjectMapper();
        try {
            return objectMapper
                .writeValueAsString((PurchaseOrderFormType) modelValue);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
}

此处未包含 PurchaseOrder 类以简化问题。这只是一个POJO。

我尝试使用下面的命令行运行器在我的 spring boot 应用程序中加载表单类型,但是,这似乎为时已晚,因为当我在由 spring-boot 自动加载的工作流中使用此类型时,类型尚未加载并抛出异常。此异常是在调用 initProcessEngine 之前引发的,我已经在调试器中验证了这一点。

@Bean
public CommandLineRunner initProcessEngine(final ProcessEngineConfiguration processEngineConfiguration) {
    return new CommandLineRunner() {
        @Override
        public void run(String... strings) throws Exception {
            List<AbstractFormType> customFormTypes = new ArrayList<AbstractFormType>();
            customFormTypes.add(new PurchaseOrderFormType());
        }
    };
}

如何使用 Spring boot XML 免费配置执行链接文章中提供的等效配置?

<bean id="processEngineConfiguration" ... >
  ...
  <property name="customFormTypes">
    <list>
      ...
      <bean class="com.marc.PurchaseOrderFormType"/>
    </list>
  </property>
</bean>

我也使用InitializingBean取得了成功:

@Bean
public InitializingBean activitiConfigurer(SpringProcessEngineConfiguration engineConfiguration) {
  return () -> engineConfiguration.setCustomFormTypes(customFormTypes);
}

Java 1.7 版本:

@Bean
public InitializingBean activitiConfigurer(SpringProcessEngineConfiguration engineConfiguration) {
  return new InitializingBean() {
    @Override
    public void afterPropertiesSet() throws Exception {
      engineConfiguration.setCustomFormTypes(customFormTypes);
    }
  };
}

我不熟悉 Aciviti 的引导集成(可能有更好的方法可以做到这一点),但您可以使用在配置类中声明为Bean BeanPostProcessor来应用配置:

@Bean
public BeanPostProcessor activitiConfigurer() {
    return new BeanPostProcessor() {
        @Override
        public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
            if (bean instanceof SpringProcessEngineConfiguration) {
                List<AbstractFormType> customFormTypes = Arrays.<AbstractFormType>asList(new PurchaseOrderFormType());
                ((SpringProcessEngineConfiguration)bean).setCustomFormTypes(customFormTypes);
            }
            return bean;
        }
        @Override
        public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
            return bean;
        }           
    };
}

最新更新