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

How to declare an Activiti custom FormType in a Spring Boot application

我有一个 Spring 启动应用程序,它是使用 Activiti 框架的 运行 工作流。我已经到了需要创建新 FormType 的地步,以便我可以支持工作流中比 String/Long 等更复杂的字段

我关注的文章是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;
    }
}

采购订单 class 未包含在此处以简化问题。它只是一个 POJO。

我尝试使用下面的命令行运行器在我的 spring 启动应用程序中加载表单类型,但是,这似乎为时已晚,因为当我在自动加载的工作流中使用此类型时通过 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 引导 XML 免费配置执行链接文章中提供的以下配置的等效配置?

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

我不熟悉 Aciviti 的引导集成(可能有更好的方法),但您可以在配置中使用声明为 BeanBeanPostProcessor 来应用配置 class:

@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;
        }           
    };

}

我也成功地使用了 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);
    }
  };
}