An application may have certain features that are applicable across multiple modules. These include logging, security, transaction management, caching etc. and are commonly referred to as cross-cutting concerns. Spring provides a method to separate these cross cutting concerns as aspects and use them declaratively wherever required. There are chiefly two ways to declare and define aspects – XML and annotations.
Although declaring aspects using XML is easy, some people may like to directly annotate classes and keep XML to a minimum. It is possible to declare expects by annotating java classes. The following annotations are possible:
- @Aspect – Declare that a class is an aspect.
- @Pointcut – Define a pointcut. The pointcut is defined over a method and the name of the method becomes the name of the pointcut.
- @Before – Declare that a method is a pointcut before a method call.
- @AfterReturning – Declare that a method is an advice after a method returns
- @AfterThrowing – Declare that a method is an advice after a method throws an error
Spring needs to know that the class is an aspect. This can be done by adding the following line in spring XML.
Sample Program Overview
This sample program demonstrates annotation based aspects. The example contains a Person class and a PersonService that has a method addPerson(). The class MethodExecutionTimeAdvice is declared an advice by annotating it with AspectJ like annotation.
We will create the Person class with members as name and email.
We will create the PersonService class with a single method addPerson()
We will create the MethodExecutionTimeAdvice class with method that provides around advice.
We will also create the spring-config.xml.
Finally, we will test our setup using TestAopAnnotation class which will load Spring context and get a reference to personService class. We will print the time taken by the addPerson method.
Create the Person (see sample code below).
Create members name and email (see lines 5-6 below)
Create accessor methods for name and email (see lines 11-19 below).
Create the PersonService (see sample code below).
Create addPerson() method which simulates adding of Person to the system (see lines 5-7 below)
Create the MethodExecutionTimeAdvice (see sample code below).
annotation to let Spring know that this Advice should be invoked (see line 7 below)
method (see line 11 below). This method is only a placeholder for the pointcut defined by
(see line 10 below). The value of the
annotation shows that the Advice should be invoked for ‘PersonService.addPerson’ method
Create calculateMethodExecutionTime() method which takes
as argument (see lines 15-25 below)
annotation (see line 14 below) with value ‘methodExecutionTimePointcut()’ indicating the pointcut method.
Add the logic to calculate the time taken to execute the method (see lines 17, 23 and 24 below)
Invoke the called method (in this case PersonService.addPerson()) using
(see line 19 below)
This demonstrates the Spring AOP configuration using AspectJ based annotations
Create the spring-config.xml file (see below).
Define the XML namespaces for aop schema (sees line 4, 9 and 10)
Enable AspectJ based autoproxying (see line 14 below)
Declare bean for personService (see line 16 below).
Declare the bean for methodExecutionTimeAdvice (see line 18 below)
Finally, we need a java program to test the our setup.This is done by TestAopAnnotation (see source code below). We need to tell Spring framework to use the ‘spring-config.xml’ to load our beans (see line 11 below).
We get the reference to PersonService class through Spring using the bean name ‘personService’ (see line 12 below).
We then create an instance of Person class with its name and email address (see lines 13-15 below).
We access the PersonService.add() method to verify that MethodExecutionTimeAdvice was successfully invoked and that it calculated the time taken to execute the PersonService.add() method (see line 16 below).
This sample program has been packaged as a jar installer which will copy the source code (along with all necessary dependencies) on your machine and automatically run the program for you as shown in the steps below. To run the sample program, you only need Java Runtime Environment (JRE) on your machine and nothing else.
(Alternatively you can go the folder containing the springaopannotation-installer.jar and execute the jar using
java -jar springaopannotation-installer.jar
This source code for this program is downloaded in the folder specified by you (say, C:\Temp) as an eclipse project called
. All the required libraries have also been downloaded and placed in the same location. You can open this project from Eclipe IDE and directly browse the source code. See below for details of the project structure.
1 thought on “Spring AOP – Annotations”
I am not able to see the download link of jar file.( which contains the source code)