定义注解
@Target( ElementType.METHOD)
@Retention( RetentionPolicy.RUNTIME)
public @interface FlowStart { /*** 流程类型*/String flowType( ) ; /*** uuid*/String uuid( ) ; /*** 是否删除原来流程*/boolean deleteOld( ) default false ;
}
定义切面
/*** 流程启动切面* @author GuoYong* @since 2023 /9/6*/
@Aspect
@Component
public class FlowStartAspect { private final ProcessInstanceClient processInstanceClient; public FlowStartAspect( ProcessInstanceClient processInstanceClient) { this.processInstanceClient = processInstanceClient; } /*** 定义切面*/@Pointcut( "execution(@com.config.FlowStart * *(..))" ) private void startAspect ( ) { } @Around( "startAspect()" ) public Object around( ProceedingJoinPoint joinPoint) throws Throwable { Object[ ] args = joinPoint.getArgs( ) ; Object proceeded = joinPoint.proceed( args) ; // 获取注解FlowStart flowStart = getAnnotationLog( joinPoint) ; // 获取注解的值String flowType = flowStart.flowType( ) ; String uuidEl = flowStart.uuid( ) ; boolean deleteOld = flowStart.deleteOld( ) ; // 解析el表达式ExpressionParser parser = new SpelExpressionParser( ) ; LocalVariableTableParameterNameDiscoverer discoverer = new LocalVariableTableParameterNameDiscoverer( ) ; Method targetMethod = getTargetMethod( joinPoint) ; String[ ] params = discoverer.getParameterNames( targetMethod) ; EvaluationContext context = new StandardEvaluationContext( ) ; for ( int len = 0 ; len < params.length; len++) { context.setVariable( params[ len] , args[ len] ) ; } Expression expression = parser.parseExpression( uuidEl) ; String uuids = expression.getValue( context, String.class) ; String [ ] uuidArray = uuids.split( "," ) ; for ( String uuid : uuidArray) { if ( deleteOld) { processInstanceClient.deleteByBusinessKey( uuid) ; } Result result = processInstanceClient.init( InitProcessDto.builder( ) .businessKey( uuid) .processKey( flowType) .build( )) ; if ( ! result.isOk( )) { throw new RuntimeException( "流程启动失败" ) ; } } return proceeded; } private Method getTargetMethod( JoinPoint joinPoint) { Signature signature = joinPoint.getSignature( ) ; MethodSignature methodSignature = ( MethodSignature) signature; Method method = methodSignature.getMethod( ) ; return method; } /*** 是否存在注解,如果存在就获取*/private FlowStart getAnnotationLog( JoinPoint joinPoint) { Method method = getTargetMethod( joinPoint) ; if ( method != null) { return method.getAnnotation( FlowStart.class) ; } return null; } }
使用
@FlowStart( flowType = ProcessKeyConstants.DUTY_SCHEDULING_FLOW, uuid = "#editDutySchedulingDto.businessKey" , deleteOld = true )