Lightweight Embedded Process Flow Using AOP with Spring and AspectJ
This article explains how to build a highly configurable, extensible, and maintainable embedded process flow by leveraging Spring AOP and AspectJ to create a lightweight alternative to heavyweight BPM engines, detailing the architecture, design, code examples, and a purchase‑item use case.
The article introduces the need for a lightweight, embedded process‑flow engine that can be easily configured, extended, and maintained without the overhead of a full BPM system. It proposes using Aspect‑Oriented Programming (AOP) techniques, specifically Spring AOP and AspectJ, to assemble and orchestrate such flows.
It defines the essential elements of a process: activities, shared data/context, transition rules, execution decisions, and optional initialization data. Activities are stateless workers that operate on a token representing shared data.
The Activity interface is defined as:
public interface Activity {
public void process(Object data);
}The shared data token can be any object, commonly a java.lang.Object or a structured type like Map.
Process execution is modeled as a chain of intercepting filters (activity filters). Each filter can pre‑process, redirect, post‑process, or replace the request/response, similar to the Intercepting Filter pattern used in servlet filters.
Two AOP layers are employed:
Functional AOP Layer (Spring AOP) : assembles the ordered filter chain and delegates to POJO activities.
Non‑Functional AOP Layer (AspectJ) : evaluates fact‑based transition rules to decide whether a filter (activity) should execute.
The GenericProcess class serves as the target object for the filter chain:
public class GenericProcess {
public void process(Object obj){
System.out.println("executing process");
}
}An around advice in TransitionGovernorAspect compares registered facts with rule conditions to either proceed with or skip the activity filter.
A concrete use case, Purchase Item , demonstrates three activities: validate item quantity, obtain credit authorization, and delivery. Facts such as VALIDATED_ITEM and CREDIT_AUTHORIZED control the flow, allowing activities to be skipped or resumed without manual orchestration.
Configuration snippets (shown as images in the source) illustrate:
Spring AOP pointcut definition for GenericProcessImpl.execute(..).
Three ActivityFilterInterceptor beans linking POJO activities with fact rules.
Bean definitions for the POJO activities themselves.
Testing involves obtaining the ApplicationContext, creating a fact registry, preparing a context map, and invoking process(). The test is run with the JVM -javaagent:lib/spring-agent.jar option to enable load‑time weaving.
Results show that when the fact registry is empty, activities execute sequentially; when facts are pre‑registered (e.g., uncommenting // factRegistry.add("VALIDATED_ITEM");), the corresponding activity is skipped, demonstrating dynamic flow control.
The conclusion emphasizes that a two‑layer AOP approach provides a lightweight, embedded process engine without requiring a separate BPM system, offering loose coupling, easy testing, OSGi compatibility, and reusability of activities.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Art of Distributed System Architecture Design
Introductions to large-scale distributed system architectures; insights and knowledge sharing on large-scale internet system architecture; front-end web architecture overviews; practical tips and experiences with PHP, JavaScript, Erlang, C/C++ and other languages in large-scale internet system development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
