Step 1: Add maven dependency. pom.xml should look like this.
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>spring_AOP</groupId>
<artifactId>spring_AOP</artifactId>
<version>0.0.1-SNAPSHOT</version>
<properties>
<!-- Generic properties -->
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<!-- Spring -->
<spring-framework.version>5.1.3.RELEASE</spring-framework.version>
<logback.version>1.0.13</logback.version>
<slf4j.version>1.7.5</slf4j.version>
<aspectj.version>1.8.9</aspectj.version>
</properties>
<dependencies>
<!-- Spring IOC start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<!-- Spring IOC end -->
<!-- Spring AOP jars...starts -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>${spring-framework.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>rg.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<!-- Spring AOP jars...end -->
</dependencies>
</project>
Step 2: write any class (Joint Point)
package com.javabykiran;
public class Invoice {
public void receivePayment() {
System.out.println("I am in receive payment");
}
public void returnCash() {
System.out.println("I am in return cash");
}
}
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
package com.javabykiran;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class AspectCode {
@Before("execution(* com.javabykiran.Invoice.*())")
public void logStartingTime() {
System.out.println("Time to start : " + new java.util.Date());
}
@After("execution(* com.javabykiran.Invoice.*())")
public void logExitTime() {
System.out.println("Time to exit : " + new java.util.Date());
}
//try below
@After("execution(* com.javabykiran.Invoice.receive*())")
public void logExitTime() {
System.out.println("Time to exit : " + new java.util.Date());
}
}
Step 4: write config class for configuration
@EnableAspectJAutoProxy to enable aop
@bean must include AspectJ class as well- AspectCode bean
package com.javabykiran;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
public class ConfigClass {
@Bean
Invoice invoiceBean() {
return new Invoice();
}
@Bean
AspectCode ascpectBean() {
return new AspectCode();
}
}
Step 5: Client code
package com.javabykiran;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class ClientAOP {
public static void main(String[] args) {
AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext(ConfigClass.class);
Invoice invoice = context.getBean(Invoice.class);
System.out.println(invoice);
invoice.receivePayment();
invoice.returnCash();
}
}
Step 6: See output
com.javabykiran.Invoice@dd05255
Time to start : Sat Dec 29 19:09:22 IST 2018
I am in receive payment
Time to exit : Sat Dec 29 19:09:22 IST 2018
Time to start : Sat Dec 29 19:09:22 IST 2018
I am in return cash
Time to exit : Sat Dec 29 19:09:22 IST 2018