AspectJ

AspectJ

AspectJ is well known in 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, and so on.

Spring is supporting AspectJ in the following two ways.

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

In declarative approach, we will use Aspectj Namespace tags in order to declare aspects, advices, Pointcuts, etc.

In 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 “id” attribute will take Aspect id value.
  • Where “ref” attribute will take identity of the class which has declared in the configuration file by using tag in out side of 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 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 methods of EmployeeService interface which are existed 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 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 second.
  • Declaring Advices
Declaring Advices

Spring AspectJ is supporting the following five advices .

  1. It is applied before calling the actual business logic method.
  2. It is applied after calling the actual business logic method.
  3. it is applied after calling the actual business logic method. It can be
    used to intercept the return value in advice.
  4. It is applied before and after calling the actual business logic method.
  5. It is applied if actual business logic method throws exception.

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

2. @AspectJ annotation style approach

Spring Framework supporting 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 result.
  5. @AfterReturning: It declares after returning advice, It will be executed after calling the actual Business method and after returning 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 exception.

Note

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

AspectJ


Scroll to top