Spring AspectJ Pointcut Expressions

0

A pointcut expressions is used to match the join points by their signatures. In this article, we look into the understanding of writing pointcut expressions and the binding of advice parameters.
Let’s get started with examples.

Spring AspectJ Pointcut Expression Examples

In order to demonstrate the different styles of pointcut expressions, we will rely on the below employee bean.

Employee:

package com.javarticles;
public class Employee {
    private String name;
    private int age;
    private String[] skills;
    
    public void setAge(int a) {
        System.out.println("setAge(" + a + ") called");
        this.age = a;
    }

    public void setName(String name) {
        System.out.println("setName(" + name + ") called");
        this.name = name;
    }
    
    public void update(int age, String name) {
        this.age = age;
        this.name = name;
    }
    
    public void update(int age, String name, String...skills) {
        this.age = age;
        this.name = name;
        this.skills = skills;
    }

    public String getName() {
        return name;
    }       

    public int getAge() {
        return age;
    }       
    
    public String[] getSkills() {
        return skills;
    }
}
  1. The following pointcut expression matches public method getAge(). Here we have used the complete signature is used.

    @Before("execution(public int getAge())")
  2. If you want to make sure only Employee.getAge() is called, you can use the below.
    @Before("execution(* com.javarticles.Employee.getAge())")
  3. Since in our case, the target class Employee and aspect class AspectExpressionsBean are both located in the same package, we can omit the package name.
    @Before("execution(* Employee.getAge())")
  4. If you want to match any method from Employee bean then use
    @Before("execution(* com.javarticles.Employee.*(..))")
  5. The preceding wildcard matches methods with any modifier (public, protected, and private) and any return type.
    @Before("execution(* getAge())")
  6. The two dots in the argument list match any number of arguments.
    @Before("execution(void set*(..))")

AspectExpressionsBean:

package com.javarticles;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class AspectExpressionsBean {

    @Before("execution(public int getAge())")
    public void fullSignatureExpression() {
        System.out
                .println("execution(public int getAge())->fullSignatureExpression()");
    }

    @Before("execution(int getAge())")
    public void withoutModifierMethodExpression() {
        System.out
                .println("execution(int getAge())->withoutModifierMethodExpression()");
    }

    @Before("execution(* getAge())")
    public void anyReturnMethodExpression() {
        System.out
                .println("execution(* getAge())->anyReturnMethodExpression()");
    }

    @Before("execution(* getAg*())")
    public void methodNameWithRegularExpression() {
        System.out
                .println("execution(* getAg*())->methodNameWithRegularExpression()");
    }
    
    @Before("execution(void set*(int))")
    public void anySetterMethodWithArgumentTypes() {
        System.out.println("execution(void set*(int))->anySetterMethodWithArgumentTypes()");
    }

    @Before("execution(void set*(..))")
    public void anySetterMethod() {
        System.out.println("execution(void set*(..))->anySetterMethod()");
    }

    @Before("execution(* *.*(*))")
    public void singleParamMethod() {
        System.out.println("execution(* *.*(*))->singleParamMethod()");
    }

    @Before("execution(* *.*(*, *))")
    public void dualParamMethod() {
        System.out.println("execution(* *.*(*, *))->dualParamMethod()");
    }

    @Before("execution(* *.*(*, ..))")
    public void anyNoOfParamMethod() {
        System.out.println("execution(* *.*(*, ..))->anyNoOfParamMethod()");
    }

    @Before("execution(!void *.*())")
    public void anyMethodReturningValue() {
        System.out.println("execution(!void *.*())->anyMethodReturningValue()");
    }

    @Before("execution(* com.javarticles.Employee.*(..))")
    public void anyMethod() {
        System.out
                .println("execution(* com.javarticles.Employee.*)->anyMethod()");
    }

    @Before(value = "execution(* com.javarticles.Employee.update(..)) && target(emp) && args(age,name)", argNames = "emp,age,name")
    public void methodWithTargetBeanAndArguments(Employee emp, int age,
            String name) {
        System.out
                .println("execution(* com.javarticles.Employee.update(..) && target(emp) && args(a,n)->methodWithTargetBeanAndArguments("
                        + emp + "," + age + "," + name + ")");
    }
}

SpringAspectPointcutExpressionsExample:

package com.javarticles;

import org.springframework.context.support.ClassPathXmlApplicationContext;

public class SpringAspectPointcutExpressionsExample {
    public static void main(String[] args) throws NoSuchMethodException, SecurityException {
        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        try {
            Employee employee = (Employee) context.getBean("emp");
            System.out.println("Call employee.setAge(32)");
            employee.setAge(32);
            System.out.println("Call employee.getAge()");
            System.out.println("employee.getAge()=" + employee.getAge());
            System.out.println("Call employee.update(34, 'Sam')");
            employee.update(34, "Sam");
            System.out.println("Call employee.update(34, 'Sam', 'Java', 'Scala')");
            employee.update(34, "Sam", "Java", "Scala");
        } finally {
            context.close();
        }
    }
}

Output:

Call employee.setAge(32)
execution(* com.javarticles.Employee.*)->anyMethod()
execution(* *.*(*, ..))->anyNoOfParamMethod()
execution(void set*(..))->anySetterMethod()
execution(void set*(int))->anySetterMethodWithArgumentTypes()
execution(* *.*(*))->singleParamMethod()
setAge(32) called
Call employee.getAge()
execution(* com.javarticles.Employee.*)->anyMethod()
execution(!void *.*())->anyMethodReturningValue()
execution(* getAge())->anyReturnMethodExpression()
execution(public int getAge())->fullSignatureExpression()
execution(* getAg*())->methodNameWithRegularExpression()
execution(int getAge())->withoutModifierMethodExpression()
employee.getAge()=32
Call employee.update(34, 'Sam')
execution(* com.javarticles.Employee.*)->anyMethod()
execution(* *.*(*, ..))->anyNoOfParamMethod()
execution(* *.*(*, *))->dualParamMethod()
execution(* com.javarticles.Employee.update(..) && target(emp) && args(a,n)->methodWithTargetBeanAndArguments([email protected],34,Sam)
Call employee.update(34, 'Sam', 'Java', 'Scala')
execution(* com.javarticles.Employee.*)->anyMethod()
execution(* *.*(*, ..))->anyNoOfParamMethod()

Download the source code

This was an example about Spring AspectJ Pointcut Expressions.

You can download the source code here: springAspectjPointcutExpressionsExample.zip
Share.

Comments are closed.