Java has not provided any support in building java programs based on AOP principles, so there are lot of third-party vendors provided various different libraries to support developing AOP-based applications aspart of java language.
1. AspectJ
2. Google Guice
3. JBoss AOP
4. Spring AOP
5. AspectWerkZ
6. Nanning
7. Dynaop
8. JAC (Java Aspect Components)
- AOP is not an replacement of oop paradigm in building the software applications, rather aop works in conjunction with oop principles in managing the cross-cutting logic that we write using oop.
- Spring AOP module has provided bunch of api classes in support of building aop applications aspart of spring framework, but if we write our application classes by referring Spring Framework provide aop api classes, our code will become invasive (tighly coupled with spring framework), so to help us in making our application non-invasive spring has added the support for AspectJ Library.
- Now instead of writing the application classes with spring aop api classes, we can very well write our application classes using AspectJ API classes and pass it as an input to Spring Framework runtime (ioc), spring can interpret the classes written using AspectJ api and apply relevant functionality equivalently the same way if we have written the code using spring aop. Thus our application classes becomes non-invasive.
How many ways of working with spring aop are there?
There are 3 ways are there
1. Programmatic aop approach (using spring aop api) (#1 eg.. per usecase aop) (grabing internals/basics of aop)
2. Declarative AspectJ Aop approach (converting prog to aspectj)
3. Annotation-driven AspectJ Aop approach (annotation [multiple eg..])
Logging
The logging usecase we have considered can be implemented using aop, but here we have done only flow tracing but we cannot implement the logging for the whole application using aop, because its not that we write log statements only for entry/exit of the methods, we need to write log statements within the program logic as well (between the method code also)
Method Before Advice:
---------------------
if we want to apply the cross-cutting logic before the target class method executes then we need to use the method before advice.
audit: keeping track of the activities that are taking place aspart of the application is called "auditing".
auditing helps us in eliminating the fraud and increases the security/protection of the business system.
For eg.. in a loan processing system, we want to capture the information about who is the person approving a load, this can be done by using auditing as below.
// user must be logged-in to the system/application (logged-in: robert)
class LoanManager {
public boolean approveLoan(long loanApplicationNo) {
System.out.println("approveLoan("+loanApplicationNo+") by rober on date : "+ LocalDateTime.now().toString());
// check for the eligibility based on the loan application
return true;
}
}
- now in the above class, we want to capture the user who has logged-in and approving/rejecting the loan in the system through auditing. the audit information is usually written into an audit schema (tables), but for sake of simplicity lets write it as an log/sysout statement.
- since audit is a secondary logic and even cross-cutting logic, having it written directly aspart of primary business logic of the application is not recommended rather using aop
class LoanManager {
public boolean approveLoan(long loanApplicationNo) {
// check for the eligibility based on the loan application
return true;
}
}
class AuditAdvice implements MethodBeforeAdvice {
public void before(Method method, Object[] args, Object proxy) {
String methodName = null;
Object[] args = null;
methodName = method.getName();
System.out.print(methodName+"(");
for(int i=0;i<args.length;i++) {
if(i == 0) {
System.out.print(args[i]);
continue;
}
System.out.print(","+args[i]);
}
System.out.println(") has been called by Robert");
}
}
1. To apply the aspect logic before the target class method executes we need to write our advice class implementing from MethodBeforeAdvice and override before(...) method .
2. within the before() method we need to write the cross-cutting logic that should be executed onbehalf of the target class method, so it needs the information about targetClass method which is passed as parameters (Method, object[], proxy).
Test.java
---------
ProxyFactory pf = new ProxyFactory();
pf.setTarget(new LoanManager());
pf.addAdvice(new AuditAdvice());
LoanManager proxy = (LoanManager) pf.getProxy();
boolean approved = proxy.approveLoan(19393);
Flow of execution:
1. since we have applied the MethodBeforeAdvice, spring aop understands we wanted to execute the aspect logic just before executing the target class method. so upon calling the proxy.approveLoan(..) method, before entering into the target class method, the control enters into the aspect advice method before() of AuditAdivce.
once the AuditAdvice before(..) method completed execution, automatically the control goes to the target class method, so the before is not being called instead of the target class method, the before is called just before the target class method, so it is not responsible to return the return value of the target, since the target method executes by itself and returns the returnValue.
So the before method returnType is void. similarly once the before method completed execution automatically the control goes to targetClass method, we dont need to pass it manually/explicity, so we dont need control over target class method, we just need info of target class method to perform cross-cutting logic,due to this there is no methodInvocation parameter.
How many control points?
1. we can modify the parameters and these values are reflected in executing the actual method incase of spring aop, because spring aop passes the original argments by reference to advice method
2. here we dont have control over executing the target class method, but we can abort execution of target class method by throwing exception in the advice method
3. the control never returns to the advice method after completing the target class method, because it is before advice, so there is no way of seeing the returnValue or modifying returnValue or returning the modified returnValue.
Comments
Post a Comment