Loading
Example for AOP
Let us create a simple Spring project to understand how Aspect-Oriented Programming (AOP) works in real applications.



Step 1: Create a Maven Project

Use Eclipse or Spring Tool Suite (STS) to create a Maven project


FieldValue
Group Idorg.apache.maven.archetypes
Artifact Idmaven-archetype-quickstart
Version1.1



Step 2: Add Required Dependencies in pom.xml

Add Spring and AspectJ dependencies

<dependencies>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>5.3.10</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>5.3.10</version>
    </dependency>
    <dependency>
        <groupId>org.aspectj</groupId>
        <artifactId>aspectjweaver</artifactId>
        <version>1.9.7</version>
    </dependency>
</dependencies>



Step 3: Create Spring Configuration File
config.xml

This file configures Spring beans and enables AspectJ AOP


<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xsi:schemaLocation="
           http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/aop
           http://www.springframework.org/schema/aop/spring-aop.xsd">

    <aop:aspectj-autoproxy/>

    <!-- Bean definitions -->
    <bean name="purchase" class="com.aop.aopexample.serviceimpl.PurchaseServiceImpl"/>
    <bean name="aspect" class="com.aop.aopexample.aspect.Aspect"/>
</beans>



Step 4: Crate the Service Class

File: PurchaseServiceImpl.java


package com.aop.aopexample.serviceimpl;

public class PurchaseServiceImpl {
    public void buyNow() {
        System.out.println("Added to Cart!!");
        System.out.println("Order Placed!!");
    }
}



Step 5: Create the Aspect Class

File: Aspect.java


package com.aop.aopexample.aspect;

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Before;

@org.aspectj.lang.annotation.Aspect
public class Aspect {

    @Before("execution(* com.aop.aopexample.serviceimpl.PurchaseServiceImpl.buyNow())")
    public void printBefore() {
        System.out.println("Please Signin");
    }

    @After("execution(* com.aop.aopexample.serviceimpl.PurchaseServiceImpl.buyNow())")
    public void printAfter() {
        System.out.println("Please Signout");
    }
}



Step 6: Main Application Class

File: App.java


package com.aop.aopexample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.aop.aopexample.serviceimpl.PurchaseServiceImpl;

public class App {
    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("com/aop/aopexample/config.xml");
        PurchaseServiceImpl purchaseObject = context.getBean("purchase", PurchaseServiceImpl.class);
        purchaseObject.buyNow();
    }
}



Step 7: Run the Application

Run App.java


Here is what happens

  • Before Advice runs first - Please Signin
  • Then the actual method - 

Added to Cart!!
Order Placed!!

  • After Advice runs last - Please Signnout

Output

Please Signin Added to Cart!! Order Placed!! Please Signout



Conclusion

Using AOP, we added logging behavior (Please Signin / Please Signout) without changing the core business method (buyNow()).


This makes our code

  • Cleaner
  • Easier to maintain
  • Better separated by concern