# Spring AOP (explanation & example)

In 
Published 2022-12-03

This tutorial gives you a short explanation of the Spring AOP concept and provides you with a simple-to-understand example.

Aspect-Oriented Programming (AOP) complements Object-Oriented Programming (OOP) by providing another way of thinking about program structure. The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity is the aspect. Aspects enable the modularization of concerns such as transaction management that cut across multiple types and objects.

The main idea behind AOP is that an application is a SUM of concerns:

  • Business concerns (no cross-cutting)
  • Technical concerns (like, logging, caching, security, etc): these concerns are applied to all the business concerns, so, these concerns are cross-cutting.

In order to create an AOP example, you have to create a Spring Maven simple project.

After that you have to add the following dependencies into the pom.xml file:

After that you have to create the following classes:

package com.example.aspects;
 
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
 
@Aspect
public class MyAspect {
 
    @Before("execution(* com.example.services.MyService1.methodA(..))")
    public void before() {
        System.out.println("This will run BEFORE methodA()");
    }
     
    @After("execution(* com.example.services.MyService1.methodA(..))")
    public void after() {
        System.out.println("This will run AFTER methodA()");
    }
}
package com.example.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
 
import com.example.aspects.MyAspect;
import com.example.services.MyService1;
 
@Configuration
@EnableAspectJAutoProxy
public class SpringContextConfig {
 
    @Bean
    public MyService1 myService() {
        return new MyService1();
    }
     
    @Bean
    public MyAspect myAspect() {
        return new MyAspect();
    }
}
package com.example.services;
 
public class MyService1 {
 
    public void methodA() {
         
        System.out.println("MyService1 is running methodA() ...");
    }
}
package com.example.main;
 
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
 
import com.example.config.SpringContextConfig;
import com.example.services.*;
 
public class Main {
 
   public static void main (String [] args) {
     
      System.out.println("Start Java Application ...");
         
      //The Spring context is configured from a Java Class for a Java Application 
      // (Not for a Web Application)
      // FileSystemXmlApplicationContext --> used when the context is defined in an xml file.
      try (AnnotationConfigApplicationContext context 
              = new AnnotationConfigApplicationContext(SpringContextConfig.class)) {
             
         System.out.println("Spring context created ...");
          
         //GET an instance from context BY CLASS
         // SINGLETON by default in Spring
         MyService1 myService1 =  context.getBean(MyService1.class);
          
         myService1.methodA();
      }
   }
}

When you run the project you must see something like that: