如图所示,flowable-spring-boot-starter-process-rest会引入flowable-rest和flowable-spring-boot-starter-process, 而flowable-spring-boot-starter-process还会进一步引入flowable-spring-boot-autoconfigure。

其中,flowable-rest存放了flowable BPMN engine相关的API实现。

flowable-spring-boot-autoconfigure根据条件加载不同引擎的API

RestApiAutoConfiguration中有如下配置:
@Configuration(proxyBeanMethods = false)
    @ConditionalOnClass(RestUrls.class)
    @ConditionalOnBean(ProcessEngine.class)
    @EnableConfigurationProperties(FlowableProcessProperties.class)
    public static class ProcessEngineRestApiConfiguration extends BaseRestApiConfiguration {
        @Bean
        public ServletRegistrationBean processService(FlowableProcessProperties properties) {
            return registerServlet(properties.getServlet(), ProcessEngineRestConfiguration.class);
        }
    }

1,由@ConditionalOnBean(ProcessEngine.class)得知,该配置在配置了BPMN引擎的情况下会生效;
2,由@Bean注解知道该配置注册了一个ServletRegistrationBean,ServletRegistrationBean可以帮组注册一个Servelet。

BaseRestApiConfiguration.registerServlet代码如下
    protected ServletRegistrationBean registerServlet(FlowableServlet servletProperties, Class<?> baseConfig) {
        AnnotationConfigWebApplicationContext dispatcherServletConfiguration = new AnnotationConfigWebApplicationContext();
        dispatcherServletConfiguration.setParent(applicationContext);
        dispatcherServletConfiguration.register(baseConfig);
        DispatcherServlet servlet = new DispatcherServlet(dispatcherServletConfiguration);
        String path = servletProperties.getPath();
        String urlMapping = (path.endsWith("/") ? path + "*" : path + "/*");
        ServletRegistrationBean registrationBean = new ServletRegistrationBean(servlet, urlMapping);
        registrationBean.setName(servletProperties.getName());
        registrationBean.setLoadOnStartup(servletProperties.getLoadOnStartup());
        registrationBean.setAsyncSupported(true);
        if (multipartConfigElement != null) {
            registrationBean.setMultipartConfig(multipartConfigElement);
        }
        return registrationBean;
    }

特别注意dispatcherServletConfiguration.register(baseConfig)会将配置类注册到Spring上下文中,对应BPMN的配置类代码如下

@Import(DispatcherServletConfiguration.class)
@ComponentScan("org.flowable.rest.service.api")
public class ProcessEngineRestConfiguration {
    
    @Autowired
    protected ObjectMapper objectMapper;

    @ConditionalOnMissingBean //If we don't include this annotation, we cannot override the RestResponseFactory bean
    @Bean
    public RestResponseFactory restResponseFactory() {
        return new RestResponseFactory(objectMapper);
    }
}

由代码可以知道,该配置类会去扫描org.flowable.rest.service.api下面的Bean,这些Bean就是RestController类型的。

总结

由上面的分析,我们了解到了Flowable Rest API实现了动态加载的能力,

  1. 如果只是添加了flowable-spring-boot-starter-process,尽管该依赖也引入了flowable-spring-boot-autoconfigure,但是由于没有引入flowable-rest,所以在配置类扫描相关的路径的时候,是找不到RestController的
  2. 如果添加了flowable-spring-boot-starter-process,那么引擎和Rest API都会同时启动

水一水
39 声望5 粉丝

总结经验,提升自己