Results 1 to 5 of 5

Thread: @Before with target does no work?

  1. #1
    Join Date
    Nov 2011
    Posts
    3

    Question @Before with target does no work?

    Hi,

    Let's say my code looks like this:
    Code:
    public interface myInterface{
    void method1();
    }
    
    public class myClass implements myInterface{...}
    Now I want to use AOP to inject the myInterface.method1.
    Code:
    @Aspect
    public class myAspect{
    @Pointcut("execution(* myInterface.method1(..)) && target(t)")
    private void myPointcut(myInterface t){}
    
    @Before("myPointcut(t)")
    public void myBefore(myInterface t){...}
    }
    Above code never works. But if I change "@Before" to "@Around", and modify the signature, it works fine.

    The reason I need the "target" is that I want to modify the target object before the method invoking. I am new to AOP. I am not even sure if it is a valid use case.

  2. #2
    Join Date
    Dec 2008
    Location
    India
    Posts
    295

    Default

    try like this


    @Before("execution(* ..Sample+.sampleGenericMethod(*))")
    Enjoy
    Rohan Chauhan
    ------------------------------------------------------------------------------
    SpringSource Certified Spring 3.0 Professional


  3. #3
    Join Date
    Nov 2011
    Posts
    3

    Default

    Can you please give a complete example? I still cannot figure out how to pass the target object the my function.

    Thanks.

  4. #4
    Join Date
    Dec 2008
    Location
    India
    Posts
    295

    Default

    @Before("execution(* ..Sample+.sampleGenericMethod(*)) && target(t)")

    or


    @Before("target(com.xyz.service.AccountService )")public void audit(JoinPoint jp) { AuditCode code = auditable.value(); // ... use jp to get target}
    Enjoy
    Rohan Chauhan
    ------------------------------------------------------------------------------
    SpringSource Certified Spring 3.0 Professional


  5. #5
    Join Date
    Jul 2010
    Location
    Venice, Italy
    Posts
    709

    Default

    From what I know with @Before you can execute code before the advised method is executed but you can't change the data that is passed to the advised method. I'd use @Around for that.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •