We create an calculator interface that has four basic methods.
1 2 3 4 5 6
publicinterfaceCalculator { intadd(int i, int j); intsubtract(int i, int j); intmultiple(int i, int j); intdivide(int i, int j); }
And has two implement of of the interface.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
publicclassCalculatorImplimplementsCalculator { @Override publicintadd(int i, int j) { intresult= i + j; System.out.println("inside method: result = " + result); return result; }
@Override publicintsubtract(int i, int j) {...}
@Override publicintmultiple(int i, int j) {...}
@Override publicintdivide(int i, int j) {...} }
And another that has some logs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
publicclassCalculatorLogImplimplementsCalculator{ @Override publicintadd(int i, int j) { System.out.println("[LOG] ADD Start, Parameters are: " + i + ", " +j); intresult= i + j; System.out.println("inside method, result = " + result); System.out.println("[LOG] ADD End, result is " + result); return result; }
@Override publicintsubtract(int i, int j) {...}
@Override publicintmultiple(int i, int j) {...}
@Override publicintdivide(int i, int j) {...} }
If we use the second implement, the log code and the core code are mixed together and it’s not easy to maintain.
So we want to separate the core code and log code.
Proxy
A dynamic proxy class (simply referred to as a proxy class below) is a class that implements a list of interfaces specified at runtime when the class is created, with behavior as described below.
Without a proxy class, we call the add() directly, but with a proxy class, we can call the proxy class and make proxy call the original add() method.
And there are to kinds of proxy: static and dynamic
Static Proxy
we create a static proxy calculator class. It has an property that is the Calculator type.
In computing, aspect-oriented programming is a programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.
Terms
Cross-cutting concern: There are multiple modules that solves the same problems, like handle transaction, data cache…
Advice: the thing you need to do(write a method) on Cross-cutting concern. the method is called advice method. There are five type of advices:
Before Advice: before the target method run
After Advice: After the final method execution ends
After-returning Advice: after the method end successfully
After-throwing Advice: after the method ends abnormally
Around Advice: use try-catch to surround target method, including the above four position of advice.
Aspect: the class the encapsulate the advice method.
Target: target object
Proxy: proxy object
Joint Point: places that allowed to use advice.
Pointcut: the position that actually advice the method.
AOP based on annotation
dynamic proxy: JDK dynamic proxy and cglib dynamic proxy
When there is interfaces, use JDK dynamic proxy. It will generate an object that is the type of the interface.
If there is no interfaces, use cglib dynamic proxy. It will generate a sub object that inherit the target object.
Aspectj: Essentially a static proxy but performs as dynamic proxy.
preparation
add AOP references
create target source
interface
implement class
create an aspect class and configure it.
Pointcut
Advice type
Create a Calculator interface and its implement class. And a class LogAspect
The value of method must be exactly same with the actual method name, or you will run into error
1 2 3
Error creating bean with name 'calculatorImpl' defined in file... ... Cannot create inner bean '(inner bean)#7fcff1b9' of type [org.springframework.aop.aspectj.AspectJAfterReturningAdvice] while setting constructor argument
this means the after-returning advice has errors because we find a error with AspectJAfterReturnningAdvice . Check the error in the <aop:xxx></aop:xxx> tag.