Configure project for AOP with java Configuration-annotations

Step 1: Add maven dependency. pom.xml should look like this.


Step 2: write any class (Joint Point)


Step 3: write code for AspectJ class

  • In below code pointcut expression is
    [@Before ("execution (* com.javabykiran.Invoice.*())")]

  • Meaning of this is call this before Advice before to all methods/joint points inside Invoice class.

  • Spring AOP users are likely to use the execution pointcut designator the most often. The format of an execution expression is:

    execution(modifiers-pattern? ret-type-pattern declaring-type-pattern?name-pattern(param-pattern) throws-pattern?)

    The parameters pattern is slightly more complex: () matches a method that takes no parameters, whereas (..) matches any number of parameters (zero or more).

    Example may be like this
    [@Before ("execution (* com.javabykiran.Invoice.payCash(*,String))")]

  • The pattern (*) matches a method taking one parameter of any type, (*, String) matches a method taking two parameters, the first can be of any type, the second must be a String.

  • This class is aspect

  • This class have pointcut expressions mentioned on advice

  • These advices will perform action on jointpoints if pointcut expression satisfies.

  • execution - for matching method execution join points, this is the primary pointcut designator you will use when working with Spring AOP

  • * - modifier-pattern

  • com.javabykiran.Invoice - Full package name with class name

  • * means all methods of invoice class

  • Different patterns will be explained at the end of this chapter


Step 4: write config class for configuration

  • @EnableAspectJAutoProxy to enable aop

  • @bean must include AspectJ class as well- AspectCode bean


Step 5: Client code


Step 6: See output