AspectJ in Spring

AspectJ in Spring

In this tutorial, we are going to discuss AspectJ in Spring. AspectJ is well known in the AOP language. It provides specialized syntax to express concerns. It also provides tools to add a concern into the system and enables crosscutting concern and modularization, such as logging, error checking and handling, etc.

AspectJ in Spring

Spring is supporting AspectJ in the following two ways.

  1. Declarative approach
  2. @AspectJ annotation style approach
1. Declarative approach

In the declarative approach, we will use Aspectj Namespace tags to declare aspects, advice, Pointcuts, etc.

In the declarative configuration approach, all the aspect declarations are placed under the tag. To use AOP namespace tags, we need to import the spring-aop schema.

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:xsi="http://www.w3.org/2001/XMLSchema- instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/b eans/spring-beans.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd">
   <aop:config>
      <!-- contains aspect configuration and all method related configuration -->
   </aop:config>
</beans>

Note

The aop:config will contain all aspect configurations and all specific method-related configurations, such as around, pointcut, and so on.

1. Declaring Aspects

To declare aspects by using AspectJ namespace tags we have to use the following tags.

<aop:config>
   <aop:aspect id="--" ref="--">
       ----
   </aop:aspect>
<aop:config>
  • Where the “id” attribute will take the Aspect id value.
  • Where “ref” attribute will take the identity of the class which has been declared in the configuration file by using the tag outside of the tag.
<beans ..... >
   <aop:config>
      <aop:aspect id="loggingAspect" ref="loggingAspectBean">
         ...
      </aop:aspect>
   </aop:config>
   <bean id="loggingAspectBean" class="com.ashok.spring.aop.EmployeeCRUDLoggingAspect" />
</beans>

2. Declaring Pointcuts

A pointcut helps in determining the join points to be executed with different advices. To declare pointcuts, we have to use the following tags in the configuration file.

<aop:config>  
   <aop:aspect id="----" ref="----">
      <aop:pointcut id="----" expression="------"/>
         ...
   </aop:aspect>
</aop:config>
  • Where “id” attribute in tag will take identity to the Pointcut.
  • Where “expression” attribute in will take AspectJ expression to define expression for the business methods which are required Advices.
  • Where “expression” attribute will take “execution” function with an expression.

E.g

<aop:pointcut id="businessService" expression="execution(* com.ashok.spring.service.*.*(..))"/>

In the above code, expression will represent all java methods with any type of return type.

<aop:config>
   <aop:aspect id="loggingAspect" ref="loggingAspectBean">
      <aop:pointcut id="loggingOperation" expression="execution(* com.ashok.spring.aop.EmployeeService.*(..))" />
   </aop:aspect>
</aop:config>
<bean id="loggingAspectBean" class="com.ashok.spring.aop.EmployeeCRUDLoggingAspect" />
Examples on Pointcut Expressions

1. execution (* com.ashok.spring.aop.EmployeeService.*(..))

  • The above Expression matches all of the methods declared in the EmployeeService interface.
  • The above expression matches methods with any modifier (public, protected, and private) and any return type.
  • The two dots in the argument list match any number of arguments.

2. execution (* EmployeeService.*(..))

The above Expression matches all EmployeeService interface methods that exist in the present package with any type of access modifier and with any return type.

3. execution (public * EmployeeService.*(..))

The above Expression matches all public methods of the EmployeService interface with any return type.

4. execution (public Employee EmployeeService.*(..))

The above Expression matches all public methods of EmployeeService interface with Employee return type.

5. execution (public Employee EmployeeService.*(Employee, ..))

The above Expression matches all methods of EmployeeService interface with Employee return type and first parameter as Employee.

6. execution (public Employee EmployeeService.*(Employee, Integer))

  • The above Expression matches all public methods of EmployeeService with Employee return type and with Employee as First parameter and Integer type parameter as the second.
  • Declaring Advices
Declaring Advices

Spring AspectJ is supporting the following five advices .

  • It is applied before calling the actual business logic method.
  • It is applied after calling the actual business logic method.
  • It is applied after calling the actual business logic method. It can be used to intercept the return value in advice.
  • It is applied before and after calling the actual business logic method.
  • It is applied if the actual business logic method throws an exception.

All the above advice tags contain “method” and “pointcut-ref” attributes, where the “method” attribute will take the advice method, and “pointcut-ref” attribute will take the Pointcut reference, which we declared in the configuration file.

2. @AspectJ annotation style approach

Spring Framework supports Annotations to support AspectJ implementation in the form of “org.aspectj.lang.annotation” package.

Spring AspectJ AOP implementation provides the following annotations.

  1. @Aspect: It will declare a class as an aspect.
  2. @Pointcut: It will declare a pointcut expression.
  3. @Before: It will declare before advice. It will be executed before executing the Business method.
  4. @After: It will declare after advice. It will be executed after executing the actual Business method and before returning the result.
  5. @AfterReturning: It declares after returning advice, It will be executed after calling the actual Business method and after returning the result.
  6. @Around: It declares around advice. It will be executed before and after calling the actual Business method.
  7. @AfterThrowing: It declares the throws advice. It will be executed if the actual Business method throws an exception.

Note

To activate all the above annotations in Spring applications, we have to use the tag in the spring configuration file.

AspectJ in Spring

Scroll to top