@AspectJ Based AOP With Spring
@AspectJ Based AOP With Spring
<aop:aspectj-autoproxy/>
You will also need the following AspectJ libraries on the classpath of your application.
These libraries are available in the 'lib' directory of an AspectJ installation, otherwise
you can download them from the internet.
aspectjrt.jar
aspectjweaver.jar
aspectj.jar
aopalliance.jar
Declaring an aspect
Aspects classes are like any other normal bean and may have methods and fields
just like any other class, except that they will be annotated with @Aspect as follows
−
package org.xyz;
import org.aspectj.lang.annotation.Aspect;
@Aspect
public class AspectModule {
}
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 1/8
4/21/24, 10:18 PM @AspectJ Based AOP with Spring
</bean>
Declaring a pointcut
A pointcut helps in determining the join points (ie methods) of interest to be
executed with different advices. While working with @AspectJ-based configuration,
pointcut declaration has two parts −
The following example defines a pointcut named 'businessService' that will match the
execution of every method available in the classes under the package
com.xyz.myapp.service −
import org.aspectj.lang.annotation.Pointcut;
The following example defines a pointcut named 'getname' that will match the
execution of getName() method available in the Student class under the package
com.tutorialspoint −
import org.aspectj.lang.annotation.Pointcut;
@Pointcut("execution(* com.tutorialspoint.Student.getName(..))")
private void getname() {}
Declaring advices
You can declare any of the five advices using @{ADVICE-NAME} annotations as
given in the code snippet. This assumes that you already have defined a pointcut
signature method businessService() −
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 2/8
4/21/24, 10:18 PM @AspectJ Based AOP with Spring
@Before("businessService()")
public void doBeforeTask(){
...
}
@After("businessService()")
public void doAfterTask(){
...
}
@Around("businessService()")
public void doAroundTask(){
...
}
You can define a pointcut inline for any of the advices. Following is an example to
define inline pointcut for before advice −
@Before("execution(* com.xyz.myapp.service.*.*(..))")
public doBeforeTask(){
...
}
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 3/8
4/21/24, 10:18 PM @AspectJ Based AOP with Spring
few advices, let us have a working Eclipse IDE in place and take the following steps
to create a Spring application −
Steps Description
Add required Spring libraries using Add External JARs option as explained
2
in the Spring Hello World Example chapter.
The final step is to create the content of all the Java files and Bean
6
Configuration file and run the application as explained below.
Here is the content of Logging.java file. This is actually a sample of aspect module
which defines methods to be called at various points.
package com.tutorialspoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Around;
@Aspect
public class Logging {
/** Following is the definition for a pointcut to select
* all the methods available. So advice will be called
* for all the methods.
*/
@Pointcut("execution(* com.tutorialspoint.*.*(..))")
private void selectAll(){}
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 4/8
4/21/24, 10:18 PM @AspectJ Based AOP with Spring
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
@Before("selectAll()")
public void beforeAdvice(){
System.out.println("Going to setup student profile.");
}
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
@After("selectAll()")
public void afterAdvice(){
System.out.println("Student profile has been setup.");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
@AfterReturning(pointcut = "selectAll()", returning = "retVal")
public void afterReturningAdvice(Object retVal){
System.out.println("Returning:" + retVal.toString() );
}
/**
* This is the method which I would like to execute
* if there is an exception raised by any method.
*/
@AfterThrowing(pointcut = "selectAll()", throwing = "ex")
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
}
}
package com.tutorialspoint;
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 5/8
4/21/24, 10:18 PM @AspectJ Based AOP with Spring
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
student.printThrowException();
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 6/8
4/21/24, 10:18 PM @AspectJ Based AOP with Spring
}
}
<aop:aspectj-autoproxy/>
</beans>
Once you are done creating the source and bean configuration files, let us run the
application. If everything is fine with your application, it will print the following
message −
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 7/8
4/21/24, 10:18 PM @AspectJ Based AOP with Spring
Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
.....
other exception content
https://www.tutorialspoint.com/spring/aspectj_based_aop_appoach.htm 8/8