Java implementation of an Aspect-Oriented Programming (AOP) mechanism using the Dynamic Proxy API. This project allows for method interception at runtime to apply cross-cutting concerns—such as logging, security, and performance monitoring—without modifying the original source code.
-
Dynamic Method Interception: Use of
java.lang.reflect.Proxyto wrap target objects and intercept method calls. -
Advice Support:
-
Before Advice: Logic executed before the target method.
-
After Advice: Logic executed after the target method finishes.
-
Around Advice: Logic that intercepts the execution entirely, allowing for return value modification or exception handling.
-
-
Builder API: An
AspectBuilderto easily define target classes and associate specific advices with method signatures. -
Automated Weaving: An
AspectWeaverthat dynamically applies registered aspects to target instances. -
Factory Pattern: A centralized
Factoryfor creating builders and weavers while managing a shared registry of aspects.
To handle method interception and runtime logic, this implementation:
- Uses Dynamic Proxies: Leverages
Proxy.newProxyInstanceto wrap any object that implements at least one interface. - State Management: Uses an
InvocationHandlerto coordinate the execution flow of before, around, and after advices. - Decoupled Architecture: Advice logic is stored as
Runnabletasks mapped to specificMethodobjects.
- Java Development Kit (JDK) 17 or higher.
- Basic understanding of Reflection and Dynamic Proxies.
Clone the repository:
git clone https://github.com/evankost/aop-proxy-java.git
cd aop-proxy-java
Use the factory to define which methods should trigger specific logic.
Factory factory = new Factory();
AspectBuilder builder = factory.newAspectBuilder()
.withTargets(new Class<?>[]{ TargetService.class })
.withBeforeAdviceFor(() -> System.out.println("Method starting..."), targetMethod)
.withAfterAdviceFor(() -> System.out.println("Method finished."), targetMethod)
.build(); // Automatically registered in the factoryApply the aspect to a concrete implementation to get a proxied object.
AspectWeaver weaver = factory.newAspectWeaver();
TargetService service = new TargetServiceImpl();
// The weaver returns a proxied version of your object
TargetService proxied = (TargetService) weaver.weave(service);
// Method calls now trigger the registered advices automatically
proxied.performAction("test"); The project uses Gradle for build automation and the Spock Framework for its comprehensive test suite.
- Navigate to the project directory:
cd aop-proxy-java
2.Build the Project:
./gradlew build
- Compiles the source code and packages the project.
3.Run Tests:
./gradlew test
-
Executes all unit and integration tests.
-
Verifies security controls, performance monitoring, and exception handling.
4.View Test Results:
- Open
lib/build/reports/tests/test/index.htmlin a browser for detailed reports on execution order and interception success.
5.Clean and Rebuild:
./gradlew clean build
Academic use. Implementation of the (original repository) specification.