Announcement Announcement Module
Collapse
No announcement yet.
@Before with target does no work? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • @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
    try like this


    @Before("execution(* ..Sample+.sampleGenericMethod(*))")

    Comment


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

      Thanks.

      Comment


      • #4
        @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}

        Comment


        • #5
          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.

          Comment

          Working...
          X