2. Talk Outline
● AOP Overview
● Anatomy of AspectJ
● Spring AOP
● Java Agents Overview
● AOP Implementations
3. Talk Motivation
Everyday challenges: profiling, code coverage, HotSwap, and monitoring.
Aspect Oriented Programing (AOP) is a solution to all these problems.
AOP is a Hidden Toolkit that everyone knows, but very few use.
This talk aims to change that.
5. Aspect Oriented Programming (AOP)
Overview
AOP is Cross-Cutting Concerns paradigm
Answers Questions:
● What is my code coverage?
● Is my JVM even running?
● Ahh, do I have to restart my app again?
● How long does this process take?
19. Hidden Toolkit: AspectJ Execution
1) Switching to ‘ajc’ compiler
Aspect Process started
1: 1
2: 1
...
39: 63245986
40: 102334155
Aspect Process took: 1845 ms
2) Run function bound by the Aspect
3) Output:
25. Hidden Toolkit: Spring AOP - AspectJ Annotation
Native AspectJ Support:
1. Aspect
2. Pointcut
3. Before
4. After
5. Around
Spring AspectJ Support
26. Hidden Toolkit: Spring AOP - Schema-Based Aspects
XML Aspects:
1. Aspect
2. Pointcut
3. Before
4. After
5. Around
Spring XML-based Aspects
27. Hidden Toolkit: Spring AOP - AspectJ vs. Schema
AspectJ Annotations:
● (+) DRY by-design
● (+) Pointcut combination
● (+) Reusable outside of Spring
● (-) Requires >Java 5 support
Spring XML:
● (+) Best for Enterprise services
● (+) Summary of enabled aspects
● (-) No separation of concerns
● (-) No Pointcut combinations
Comparing Two Approaches
28. Hidden Toolkit: Spring AOP - Limitations
● No Field-level interception allowed (i.e. #set(), #get())
● Limited support for objects not managed by Spring
● No support for certain general pointcuts (ex. handler, call)
Limitation of Spring In Comparison With Native AspectJ
36. AOP Implementation: Code Coverage - Continue
● No setup required (i.e. Java Agent)
● Produces detailed reports of Code Coverage
● Highlights coverage down to code lines
● Instruments Running/Pre-Execution code
Why AOP with JaCoCo?
37. AOP Implementation: Code Coverage - Continue
● Classes are instrumented on the fly by Java Agent
a. Performs in-memory pre-processing of all files
b. Instruments using java.lang.instrument.Instrumentation
● Collects coverage data into .exec files
● Process collected data into .html reports
How It Actually Works
40. AOP Implementation: Benchmark - Continue
● Reliable measure of individual units (ex. microservices)
● Supported by Oracle
● Highly customizable:
• State
• Time Unit
• Mode
Why AOP with JMH?
42. AOP Implementation: Improved Compilation
Default Java Hotswap handles re-compilation of java files.
Does Not Handle:
● Renaming/Addition/Deletion of Methods
● Changes to Static/Non-Static Fields
● Changes to Enum Classes
● Changes to Class Hierarchy (interfaces/superclasses)
Java Hotswap
43. AOP Implementation: Improved Compilation
● Java Agent that handles imperfections of Java Hotswap.
● Can be run as a javaagent on a start of JVM:
• java -XXaltjvm=dcevm -javaagent:hotswap-agent.jar test-app.jar
● Can be attached to running JVM via com.sun.tools.attach.VirtualMachine calls
● Currently in beta version
Note: Java agents like HotswapAgent do not directly change main binaries, but
rather modify proxied copy of the binaries.
HotswapAgent[1]
44. AOP Implementation: Improved Compilation
● No setup required (i.e. Java Agent)
● Productivity Booster (especially in enterprise)
● More Control over JVM
● Verbose Logging
Why AOP with HotswapAgent?