http://cgenit.blogspot.com/2015/03/create-simple-maven-app-to-test-and.html with some modifications
We have made a simple applicatioon to test and run the AOP concept.
Here we have taken the logging aspect as a cross cutting concern.
Created LoginAspect.java and made it aspect using @Aspect and written advice using
@Before("execution(public String getName())")
The two object classes we have used is Circle and Triangle, where both having simple attribute name with getter and setter.
So we need to log it whenever the getter called.
We have created ShapeService class to get the object, for better standard way to present.
AppMain.java is used to test the application.
Pom.xml and spring.xml does not have any changes
We are changing LoginAspect.java - @Aspect class and test code changes with AppMain.java
Now here
1. We won't to write a PointCut to execute logging for all the methods in the Circle object.
We are writing
@Pointcut("execution(* * org.aop.model.Circle.*(..))")
for any access modifier
any return type
only Circle class
all the methods
with any parameter
@Pointcut("execution(* * org.aop.model.Circle.*(..))")
public void allCircleMethods(){}
But this is not much readable , we have better way of writing this
more readable way. using within
I have commented other methods
-------------------------------------------------------------------------------
package org.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoginAspect {
// @Before("allGetters()")
// public void loginAdvice() {
// System.out.println("Login advice run::get method called");
// }
//
// @Before("allGetters()")
// public void secondAdvice() {
// System.out.println("Second advice executed.");
// }
@Before("allCircleMethods()")
public void allCircleAdvice() {
System.out.println("All circle executed.");
}
// @Pointcut("execution(public * *get*())")
// public void allGetters(){}
@Pointcut("within(org.aop.model.Circle)")
public void allCircleMethods(){}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package org.aop.main;
import org.aop.service.ShapeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
// you need to cast if not use second parameter ShapeService.class
ShapeService shapeService = ctx.getBean("shapeService",
ShapeService.class);
System.out.println(shapeService.getTriangle().getName());
System.out.println(shapeService.getCircle().getName());
}
}
output Now : Note = No advice run for Triangle
===============================================================
Traingle name
All circle executed.
Circle name
===============================================================
2. Modify for all methods in the package
just replace Circle with star
@Pointcut("within(org.aop.model.*)")
public void allCircleMethods(){}
output Now : Note = advice run for Triangle
===============================================================
All circle executed.
Traingle name
All circle executed.
Circle name
===============================================================
3. There are few matches with Pointcut
eg: args()
4. You can combine Poincuts
First let's look at output before combining
we are running only get method for Circle.
--------------------------------------------------------------------------------
package org.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoginAspect {
@Before("allGetters()")
public void loginAdvice() {
System.out.println("Login advice run::get method called");
}
@Before("allGetters()")
public void secondAdvice() {
System.out.println("Second advice executed.");
}
@Before("allCircleMethods()")
public void allCircleAdvice() {
System.out.println("All circle executed.");
}
@Pointcut("execution(public * *get*())")
public void allGetters() {
}
@Pointcut("within(org.aop.model.Circle)")
public void allCircleMethods() {
}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package org.aop.main;
import org.aop.service.ShapeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
// you need to cast if not use second parameter ShapeService.class
ShapeService shapeService = ctx.getBean("shapeService",
ShapeService.class);
// System.out.println(shapeService.getTriangle().getName());
System.out.println(shapeService.getCircle().getName());
}
}
--------------------------------------------------------------------------------
output Now :
===============================================================
Login advice run::get method called
Second advice executed.
All circle executed.
Login advice run::get method called
Second advice executed.
Circle name
===============================================================
Now combine with &&
@Before("allGetters() && allCircleMethods()")
public void loginAdvice() {
System.out.println("Login advice run::get method called");
}
output after combinig with &&
===============================================================
Second advice executed.
All circle executed.
Login advice run::get method called
Second advice executed.
Circle name
===============================================================
First let's look at output before combining
we are running only get method for Circle.
--------------------------------------------------------------------------------
package org.aop.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoginAspect {
@Before("allGetters()")
public void loginAdvice() {
System.out.println("Login advice run::get method called");
}
@Before("allGetters()")
public void secondAdvice() {
System.out.println("Second advice executed.");
}
@Before("allCircleMethods()")
public void allCircleAdvice() {
System.out.println("All circle executed.");
}
@Pointcut("execution(public * *get*())")
public void allGetters() {
}
@Pointcut("within(org.aop.model.Circle)")
public void allCircleMethods() {
}
}
--------------------------------------------------------------------------------
--------------------------------------------------------------------------------
package org.aop.main;
import org.aop.service.ShapeService;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class AppMain {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext(
"spring.xml");
// you need to cast if not use second parameter ShapeService.class
ShapeService shapeService = ctx.getBean("shapeService",
ShapeService.class);
// System.out.println(shapeService.getTriangle().getName());
System.out.println(shapeService.getCircle().getName());
}
}
output Now :
===============================================================
Login advice run::get method called
Second advice executed.
All circle executed.
Login advice run::get method called
Second advice executed.
Circle name
===============================================================
Now combine with &&
@Before("allGetters() && allCircleMethods()")
public void loginAdvice() {
System.out.println("Login advice run::get method called");
}
output after combinig with &&
===============================================================
Second advice executed.
All circle executed.
Login advice run::get method called
Second advice executed.
Circle name
===============================================================
Now combine with ||
@Before("allGetters() || allCircleMethods()")
public void loginAdvice() {
System.out.println("Login advice run::get method called");
}
output after combinig with ||
===============================================================
Login advice run::get method called
Second advice executed.
All circle executed.
Login advice run::get method called
Second advice executed.
Circle name
===============================================================
No comments:
Post a Comment