I tried to give the layman code for target and this pointcut designator. It is already too late, but hoping, it would be helpful for someone.
Aspect Class
package com.opensource.kms;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class SecurityService {
@Pointcut("target(com.opensource.kms.FooDao)")
public void myPointCut1() {}
@Pointcut("this(com.opensource.kms.SooDao)")
public void myPointCut2() {}
@Before("myPointCut1()")
public void beforeMethodTarget() {
System.out.println("beforeMethodTarget myPointCut1");
}
@Before("myPointCut2()")
public void beforeMethodThis() {
System.out.println("beforeMethodThis myPointCut2");
}
}
FooDao Class : JDK dynamic proxy
package com.opensource.kms;
import org.springframework.stereotype.Component;
interface BarDao {
String m();
}
@Component
public class FooDao implements BarDao {
public String m() {
System.out.println("implementation of m");
return "This is return value";
}
}
SooDao Class : CGLIB proxy
package com.opensource.kms;
import org.springframework.stereotype.Component;
@Component
public class SooDao {
public String m() {
System.out.println("implementation of m : SooDao");
return "This is return value";
}
}
Main App
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(JavaConfig.class);
SooDao ob1 = ctx.getBean("sooDao", SooDao.class);
System.out.println("Data using this ->"+ob1.m());
System.out.println("********************");
BarDao ob2 = ctx.getBean("fooDao", BarDao.class);
System.out.println("Data using target -> "+ob2.m());
Output
beforeMethodThis myPointCut2
implementation of m : SooDao
Data using this ->This is return value
********************
beforeMethodTarget myPointCut1
implementation of m
Data using target -> This is return value
this
andtarget
do the same thing??? Once my code tries to execute some method ofAccountService
, then from the receiver point of view,this instanceof AccountService
is true; and from the caller point of viewcalledObject instanceof AccountService
is also true. So why is this redundancy? – Marisolmarissa