Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I access methods attributes with Spring AOP (AspectJ-style)?

I need to intrecept some methods and their attributes by using annotations as point cuts, but how can I access those method attributes. I have following code that succesfully can run code before method is run, but I just don't know how I can access those attrbiutes.

package my.package;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

@Aspect
public class MyAspect {

 @Pointcut(value="execution(public * *(..))")
 public void anyPublicMethod() {
 }

 @Around("anyPublicMethod() && @annotation(myAnnotation )")
 public Object myAspect(ProceedingJoinPoint pjp, MyAnnotation myAnnotation)
    throws Throwable {

  // how can I access method attributes here ?
  System.out.println("hello aspect!");
  return pjp.proceed();
 }
}
like image 667
newbie Avatar asked Oct 27 '10 07:10

newbie


People also ask

What is @AspectJ in spring AspectJ?

@AspectJ is mainly used for declaring aspects as regular Java classes annotated with annotations. Spring AspectJ AOP implementation has many annotations. They are explained below:

What is @AspectJ AOP in Java?

AOP i.e Aspect-Oriented Programming complements OOP by enabling modularity of cross-cutting concerns. @AspectJ is mainly used for declaring aspects as regular Java classes annotated with annotations. Spring AspectJ AOP implementation has many annotations.

What is the use of @AspectJ annotation in Java?

@AspectJ is mainly used for declaring aspects as regular Java classes annotated with annotations. Spring AspectJ AOP implementation has many annotations. They are explained below: @Aspect: It declares the class as an aspect. @Pointcut: It declares the pointcut expression.

What is load time weaving with AspectJ in the Spring Framework?

Finally, Section 7.8.4, “Load-time weaving with AspectJ in the Spring Framework” provides an introduction to load-time weaving for Spring applications using AspectJ. The Spring container instantiates and configures beans defined in your application context.


2 Answers

You can get them from the ProceedingJoinPoint object:

@Around("anyPublicMethod() && @annotation(myAnnotation )")
public Object myAspect(final ProceedingJoinPoint pjp,
    final MyAnnotation myAnnotation) throws Throwable{

    // retrieve the methods parameter types (static):
    final Signature signature = pjp.getStaticPart().getSignature();
    if(signature instanceof MethodSignature){
        final MethodSignature ms = (MethodSignature) signature;
        final Class<?>[] parameterTypes = ms.getParameterTypes();
        for(final Class<?> pt : parameterTypes){
            System.out.println("Parameter type:" + pt);
        }
    }

    // retrieve the runtime method arguments (dynamic)
    for(final Object argument : pjp.getArgs()){
        System.out.println("Parameter value:" + argument);
    }

    return pjp.proceed();
}
like image 151
Sean Patrick Floyd Avatar answered Oct 17 '22 17:10

Sean Patrick Floyd


ProceedingJoinPoint has pjp.getArgs(), which returns all the parameters of the method.

(but these are called parameters / arguments, not attributes)

like image 29
Bozho Avatar answered Oct 17 '22 18:10

Bozho