By kswaughs | Friday, February 12, 2016

Spring AOP with annotations

Spring AOP allows us to add multiple aspects without changing the actual business logic code of java files like controllers, service etc.

In this example, I am going to explain you how to apply aspects in a spring based application using annotations.

I am adding these examples on top of the existing spring application that is explained in below articles.

Step 1: Add @EnableAspectJAutoProxy annotation to the spring configuration file.

package com.kswaughs.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

import com.kswaughs.interceptor.TransactionInterceptor;

@ComponentScan({ "com.kswaughs.web" })
public class SpringWebConfig extends WebMvcConfigurerAdapter {
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
    public InternalResourceViewResolver viewResolver() {
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        return viewResolver;
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TransactionInterceptor())

Step 2: Create an Aspect class and define the pointcut expression where you want to apply this aspect. In this example, I am applying this aspect after PhoneService.getPhoneDetails() method is executed.

package com.kswaughs.web.aspect;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

public class PhoneLogAspect {

        pointcut = "execution(* com.kswaughs.web.service.PhoneService.getPhoneDetails(..))",
        returning = "result")
    public void logAfterReturning(JoinPoint joinPoint, Object result) {

        System.out.println("logAfterReturning() is running!");
        System.out.println("Method Intercepted : " + joinPoint.getSignature().getName());
        System.out.println("Method returned value is : " + result);

Below is the output printed in console when phone details page is rendered to the browser.

logAfterReturning() is running!
Method Intercepted : getPhoneDetails
Method returned value is : Phone [id=2, name=Nokia Lumia, price=12,000]

Maven dependencies

    <!-- Spring framework -->
    <!-- AspectJ -->
    <!-- JSTL for jsp pages -->
    <!-- Servlet API jars -->

Recommend this on

1 comment:

  1. The effectiveness of IEEE Project Domains depends very much on the situation in which they are applied. In order to further improve IEEE Final Year Project Domains practices we need to explicitly describe and utilise our knowledge about software domains of software engineering Final Year Project Domains for CSE technologies. This paper suggests a modelling formalism for supporting systematic reuse of software engineering technologies during planning of software projects and improvement programmes in Project Centers in Chennai for CSE.

    Spring Framework has already made serious inroads as an integrated technology stack for building user-facing applications. Spring Framework Corporate TRaining the authors explore the idea of using Java in Big Data platforms.
    Specifically, Spring Framework provides various tasks are geared around preparing data for further analysis and visualization. Spring Training in Chennai