Flowable在SpringBoot环境下自定义身份管理服务

实现方式参考官方配置文档官方LDAP实现

实现源码:https://github.com/iamKyun/flowable-examples/tree/master/example-1

代码实现

此实现的SpringBoot版本为2.3.0,Flowable版本为6.5,不同的版本可能实现不一样,但是最终目的都是要让我们自己的身份管理服务实现类去把Flow able自动配置的给替换掉。

  1. 实现接口:org.flowable.idm.api.IdmIdentityService,根据需求实现里面的查询器和查询方法

    // 这里直接继承了它的默认实现类
    public class CustomIdmIdentityServiceImpl extends IdmIdentityServiceImpl {
        @Override
        public UserQuery createUserQuery() {
            // 自定义的用户查询器实现
            return new CustomUserQueryImpl();
        }
    }
    
  2. 用Spring Bean的方式配置一个属于SpringIdmEngineConfiguration的引擎配置器,在配置器里设置IdmIdentityService的自定义实现

    @Bean
    public EngineConfigurationConfigurer<SpringIdmEngineConfiguration> idmEngineConfigurationConfigurer() {
    	return idmEngineConfiguration -> idmEngineConfiguration.setIdmIdentityService(
    	new CustomIdmIdentityServiceImpl());
    }
    
  3. 测试

    @SpringBootTest(classes = ExampleApplication.class)
    class ExampleApplicationTests {
    
        @Autowired
        private IdentityService identityService;
    
        @Test
        @FormDeploymentAnnotation
        public void identityServiceTest() {
            // 查询方法最终调用了 CustomUserQueryImpl,里面的测试数据有3个用户,对应ID:["1","2","3"]
            long result1 = identityService.createUserQuery().userId("1").count();
            long result2 = identityService.createUserQuery().userId("4").count();
            long result3 = identityService.createUserQuery().userIds(Arrays.asList("1", "2", "4")).count();
            Assertions.assertEquals(1, result1);
            Assertions.assertEquals(0, result2);
            Assertions.assertEquals(2, result3);
        }
    
    }
    

源码解析

org.flowable.engine.IdentityService执行查询的方法中,是代理给org.flowable.idm.api.IdmIdentityService去执行。

图1

此接口的实现只有1个org.flowable.idm.engine.impl.IdmIdentityServiceImpl

图2

查找调用处可以发现只有1处新建该类的对象 (org.flowable.idm.engine.IdmEngineConfiguration:100),所以最终的目的是:更改IdmEngineConfiguration的成员变量:idmIdentityService,让其变成我们自定义的IdmIdentityService (此处指上文的CustomIdmIdentityServiceImpl)。

图3

现在所处在的类是org.flowable.idm.engine.IdmEngineConfiguration,它是Flowable的IDM引擎配置类,在Spring环境下该类有一个子类名为:org.flowable.idm.spring.SpringIdmEngineConfiguration,而引入了flowable-spring-boot-starter之后,在SpringBoot的环境下,自动配置生效。使得SpringIdmEngineConfiguration自动成为一个Spring Bean。

图4

4-1

所以此时已经有一个方法了,只要改变此SpringIdmEngineConfiguration Bean的IdmIdentityService就可以替换IDM引擎。

4-2

这样也能达到目的,但是配置过于冗余,基本上是复制了一整段代码。

参考官方LDAP自动配置的实现,可以发现它只用一个Bean就完成了IdmIdentityService的替换,就是文章中一开始所展示的实现方式。

这种实现方式是怎么生效的呢?其中关键接口是org.flowable.spring.boot.EngineConfigurationConfigurer

由它的注释可以看到:

Interface to be implemented by a bean that does some extra configuration of a Flowable engines. If such a bean is defined, it will be called when the specific engine configuration is created and the default values have been set.

在Flowable引擎创建并设置好默认值时,这个接口是实现类会用来为这些Flowable引擎做一些额外的配置工作。

这个接口与IDM引擎关联的代码是在:org.flowable.spring.boot.idm.IdmEngineAutoConfiguration.IdmEngineAppConfiguration#idmEngineConfigurator

图5

SpringIdmEngineConfiguration 准备好时,它会调用invokeConfigurars()方法,让属于SpringIdmEngineConfiguration的配置器来为SpringIdmEngineConfiguration做一些额外的配置。

图6

最后,就调用到了我们文中一开始所定义的配置器Bean,让我们自定义的IDM服务生效。

图7

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
下面是一个示例,演示如何在Spring Boot应用程序中使用Flowable自定义表达式函数。 1. 创建一个实现`org.flowable.common.engine.impl.el.function.FlowableFunction`接口的类,例如: ```java package com.example.flowabledemo.expression; import org.flowable.common.engine.impl.el.function.FlowableFunction; import java.util.List; public class CustomFunction implements FlowableFunction { @Override public Object apply(List<Object> inputValues) { // 自定义函数的行为逻辑 if (inputValues.size() == 2) { String str1 = (String) inputValues.get(0); String str2 = (String) inputValues.get(1); return str1 + str2; } else { throw new IllegalArgumentException("Two parameters are required."); } } } ``` 2. 创建一个扩展类,继承`org.flowable.spring.boot.FlowableProcessEngineConfiguration`类,并覆盖`expressionManager()`方法,在方法中注册自定义函数,例如: ```java package com.example.flowabledemo.config; import com.example.flowabledemo.expression.CustomFunction; import org.flowable.spring.boot.FlowableProcessEngineConfiguration; import org.flowable.spring.boot.FlowableSpringBootProperties; import org.flowable.spring.boot.process.FlowableProcessProperties; import org.flowable.spring.boot.variable.FlowableVariableProperties; import org.springframework.context.annotation.Configuration; @Configuration public class FlowableConfig extends FlowableProcessEngineConfiguration { public FlowableConfig(FlowableSpringBootProperties properties, FlowableVariableProperties variableProperties, FlowableProcessProperties processProperties) { super(properties, variableProperties, processProperties); } @Override public ExpressionManager expressionManager() { ExpressionManager expressionManager = super.expressionManager(); expressionManager.registerFunction("myFunc", "concat", new CustomFunction()); return expressionManager; } } ``` 3. 在流程定义的表达式中使用自定义函数,例如: ```xml <userTask id="task1" name="Task 1"> <extensionElements> <flowable:taskListener event="create" class="com.example.flowabledemo.listener.TaskListener"> <flowable:field name="message" expression="${myVar:concat('Hello', 'World')}"/> </flowable:taskListener> </extensionElements> </userTask> ``` 在上面的示例中,我们注册了一个名为`concat`的自定义函数,并在流程定义的表达式中使用`${myVar:concat('Hello', 'World')}`来调用该函数,其中`myVar`是表达式中的变量。 需要注意的是,我们继承了`FlowableProcessEngineConfiguration`类并覆盖`expressionManager()`方法来注册自定义函数。如果你使用的是XML配置文件,则可以使用类似下面的配置来实现相同的功能: ```xml <bean id="processEngineConfiguration" class="org.flowable.spring.SpringProcessEngineConfiguration"> ... <property name="expressionManager"> <bean class="org.flowable.spring.SpringExpressionManager"> <property name="functions"> <map> <entry key="myFunc" value-ref="customFunction"/> </map> </property> </bean> </property> ... </bean> <bean id="customFunction" class="com.example.flowabledemo.expression.CustomFunction"/> ``` 以上例子中,我们将自定义函数注入到Spring容器中,并在`expressionManager()`方法中使用`SpringExpressionManager`来注册自定义函数。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值