Announcement Announcement Module
Collapse
No announcement yet.
when to use this(obj) & target(obj) to select join points ? whats different ? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • when to use this(obj) & target(obj) to select join points ? whats different ?

    PCD target() and this() intercepts any Spring managed beans public methods execution. I wonder is there any difference between those. Or its just you can do the same thing in multiple ways ?

    @Aspect
    public class MyAspect {

    //@Before("target(proxyObj)")
    @Before("this(proxyObj)")
    public void applyBeforeAdvice(Object proxyObj) {
    System.out.println("This is the advice from MyAspect ");
    System.out.println("[From Aspect]" + proxyObj.getClass().getCanonicalName());
    }
    }

    @ContextConfiguration("classpath:my/aspect/aspects-config.xml")
    @RunWith(SpringJUnit4ClassRunner.class)
    public class AspectTest {

    @Autowired
    private SomeService myService;

    @Autowired
    private AnotherService anotherService;

    @Test
    public void test() {
    anotherService.myAnotherServiceMethod();
    myService.anotherServiceMethod();
    myService.myServiceMethod();
    }

    }

  • #2
    As you see in http://www.eclipse.org/aspectj/doc/r...pointcuts.html

    this(Type or Id)
    Picks out each join point where the currently executing object (the object bound to this) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any join points from static contexts.

    target(Type or Id)
    Picks out each join point where the target object (the object on which a call or field operation is applied to) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any calls, gets, or sets of static members.


    Note that Spring AOP don't support call pointcut (all are executions) so in Spring AOP target object and current object are the same.


    Cheers

    Comment


    • #3
      Sorry the copied documentation does not exactly clarify the Spring AOP behavior. I think the statement "in Spring AOP , target and this object are same makes sense"

      Like i said both @this(someObj) or @target(someObj) intercepts ALL Spring managed beans public method execution.

      When running the test in debug mode, i can see Spring creating proxies for ALL beans that the ApplicationContext have

      -------------------------------------------------------------------------------------------
      DEBUG: org.springframework.aop.aspectj.annotation.Reflect iveAspectJAdvisorFactory - Found AspectJ method: public void my.aspect.MyAspect.applyBeforeAdvice(java.lang.Obj ect)
      DEBUG: org.springframework.aop.aspectj.annotation.Annotat ionAwareAspectJAutoProxyCreator - Creating implicit proxy for bean 'myService' with 0 common interceptors and 2 specific interceptors
      DEBUG: org.springframework.aop.framework.JdkDynamicAopPro xy - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [my.service.MyService@1b2725a]
      DEBUG: org.springframework.aop.aspectj.annotation.Annotat ionAwareAspectJAutoProxyCreator - Creating implicit proxy for bean 'anotherService' with 0 common interceptors and 2 specific interceptors
      DEBUG: org.springframework.aop.framework.JdkDynamicAopPro xy - Creating JDK dynamic proxy: target source is SingletonTargetSource for target object [my.service.AnotherServiceImpl@1225fc1]
      ------------------------------------------------------------------------------

      Originally posted by chelu View Post
      As you see in http://www.eclipse.org/aspectj/doc/r...pointcuts.html

      this(Type or Id)
      Picks out each join point where the currently executing object (the object bound to this) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any join points from static contexts.

      target(Type or Id)
      Picks out each join point where the target object (the object on which a call or field operation is applied to) is an instance of Type, or of the type of the identifier Id (which must be bound in the enclosing advice or pointcut definition). Will not match any calls, gets, or sets of static members.


      Note that Spring AOP don't support call pointcut (all are executions) so in Spring AOP target object and current object are the same.


      Cheers

      Comment


      • #4
        Note that Spring AOP don't support call pointcut (all are executions) so in Spring AOP target object and current object are the same.
        Is a Spring AOP limitation that only implements the execution pointcut where current (this) and target have the same value.

        Cheers

        Comment

        Working...
        X