//@Pointcut("execution(* *
org.yash.watertechsol.model.circle.*(..))")
//@Pointcut("within(org.yash.watertechsol.model.circle)")
@Pointcut("within(org.yash.watertechsol.model.*)")
@Pointcut("within(org.yash.watertechsol.model..*)")->with
pkg & all sub-pkgs
public
void allCicleMethods(){}
@Pointcut(args()) -> matches with arguments
If you want to run multiple pointcuts
AopMain.java
package org.yash.watertechsol;
import org.springframework.context.ApplicationContext;
import
org.springframework.context.support.ClassPathXmlApplicationContext;
import org.yash.watertechsol.service.ShapeService;
public class AopMain {
public static void main(String
args[]){
ApplicationContext
ctx = new ClassPathXmlApplicationContext("spring.xml");
ShapeService
shapeService = ctx.getBean("shapeService", ShapeService.class);
System.out.println(shapeService.getCircle().getName());
}
}
LoggingAspect.java
package org.yash.watertechsol.aspect;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
@Aspect
public class LoggingAspect {
@Before("allGetters()
&& allCicleMethods()")
public void
LoggingAdvice(){
System.out.println("Advice
run. Get method called");
}
@Before("allGetters()")
public void
secondAdvice(){
System.out.println("Second
advice execute");
}
@Pointcut("execution(*
get*())")
public void allGetters(){}
//@Pointcut("execution(*
* org.yash.watertechsol.model.circle.*(..))")
@Pointcut("within(org.yash.watertechsol.model.Circle)")
public void
allCicleMethods(){}
}
ShapeService.java
package org.yash.watertechsol.service;
import org.yash.watertechsol.model.Circle;
import org.yash.watertechsol.model.Triangle;
public class ShapeService {
private
Triangle triangle;
private
Circle circle;
public
Triangle getTriangle() {
return
triangle;
}
public
void setTriangle(Triangle triangle) {
this.triangle
= triangle;
}
public
Circle getCircle() {
return
circle;
}
public
void setCircle(Circle circle) {
this.circle
= circle;
}
}
Triangle.java
package org.yash.watertechsol.model;
public class Triangle {
private String name;
public void setName(String
name) {
this.name = name;
}
public String
getName() {
return name;
}
}
Circle.java
package org.yash.watertechsol.model;
public class Circle {
private String name;
public void setName(String
name) {
this.name = name;
}
public String
getName() {
return name;
}
}
No comments:
Post a Comment