Spring - AOP AspectJ Xml Configuration

Last Updated : 5 May, 2026

Spring AOP with AspectJ XML configuration allows developers to define aspects, pointcuts, and advice using XML instead of annotations. It provides a declarative way to separate cross-cutting concerns like logging and transaction management from business logic. This approach is useful in legacy applications or when avoiding annotation-based configuration.

  • Uses XML configuration (<aop:config>) to define aspects, pointcuts, and advice.
  • Does not require annotations like @Aspect, making it suitable for XML-based projects.
  • Supports different advice types such as before, after, around, after-returning, and after-throwing.

Why Use XML-Based AOP?

  • Keeps configuration separate from business logic
  • No need to modify source code
  • Useful for large and legacy systems

Step-by-Step Implementation for All AOP Advice Types

This section demonstrates how to configure and apply different AOP advice types—before, after, after-returning, around, and after-throwing—using AspectJ XML configuration in a Spring application.

Step 1: Create Java Project

  • Create a Spring (non-Boot) project in your IDE.
  • Add required Spring Core + AOP dependencies (spring-context, spring-aop, aspectjweaver).
 

Step 2: Create Business Class

  • Create a class containing business logic methods.
  • These methods will act as target methods for AOP.

Operation.java

Java
public class Operation {
    public void msg() { System.out.println("msg() method invoked"); }
    public int m() { System.out.println("m() method invoked"); return 2; }
    public int k() { System.out.println("k() method invoked"); return 3; }
}

File: TrackOperation.java

Java
package com.Geeksforgeeks;

import org.aspectj.lang.JoinPoint;

public class TrackOperation{
    
    public void myadvice(JoinPoint jp)//it is advice
    {
        System.out.println("additional concern");
        //System.out.println("Method Signature: "  + jp.getSignature());
    }
}

File: applicationContext.xml

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
    xmlns:xsi="https://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-3.0.xsd 
    http://www.springframework.org/schema/aop/ 
    http://www.springframework.org/schema/aop//spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="opBean" class="com.Geeksforgeeks.Operation">    </bean>
    
<bean id="trackAspect" class="com.Geeksforgeeks.TrackOperation"></bean>
        
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @Before -->
     <aop:pointcut id="pointCutBefore"    expression="execution(* com.Geeksforgeeks.Operation.*(..))" />
     <aop:before method="myadvice" pointcut-ref="pointCutBefore" />
  </aop:aspect>
</aop:config>
    
</beans>

File: Test.java

Java
// Java Program to Illustrate Application Class

package com.Geeksforgeeks;

// Importing required classes
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

// Class
public class Test {

    // Main driver method
    public static void main(String[] args)
    {

        // Creating object of ApplicationContext
        // and Operation Class
        ApplicationContext context
            = new ClassPathXmlApplicationContext(
                "applicationContext.xml");
        Operation e = (Operation)context.getBean("opBean");

        // Print statements and calling methods
        / as defined in other class above System.out.println
        ("calling msg...");
        e.msg();
        System.out.println("calling m...");
        e.m();
        System.out.println("calling k...");
        e.k();
    }
}

Output:

B. aop:after Example

After invoking the real business logic methods, the AspectJ after guidance is implemented. It may be used to keep track of logs, security, and notifications, among other things. We'll assume that the files Operation.java, TrackOperation.java, and Test.java are identical to those in the aop:before example.

Create the applicationContext.xml file, which contains the bean definitions.

File: applicationContext.xml

XML
<?xml version="1.0" encoding="UTF-8"?>  
<beans xmlns="http://www.springframework.org/schema/beans/"  
    xmlns:xsi="https://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-3.0.xsd   
    http://www.springframework.org/schema/aop/   
    http://www.springframework.org/schema/aop//spring-aop-3.0.xsd ">  
  
<aop:aspectj-autoproxy />  
  
<bean id="opBean" class="com.Geeksforgeeks.Operation">   </bean>  
<bean id="trackAspect" class="com.Geeksforgeeks.TrackOperation"></bean>  
          
<aop:config>  
  <aop:aspect id="myaspect" ref="trackAspect" >  
     <!-- @After -->  
     <aop:pointcut id="pointCutAfter"    expression="execution(* com.Geeksforgeeks.Operation.*(..))" />  
     <aop:after method="myadvice" pointcut-ref="pointCutAfter" />  
  </aop:aspect>  
</aop:config>  
      
</beans>  

Output:

We can see that additional concern is printed after calling msg(), m(), and k() methods.

C. aop:after-returning example

We may receive the outcome in the advice by using after returning advice. Make a class to hold the business logic.

File: Operation.java

Java
package com.Geeksforgeeks;
public  class Operation{
    public int m(){System.out.println("m() method invoked");return 2;}
    public int k(){System.out.println("k() method invoked");return 3;}
}

Step 3: Create Aspect Class

  • Create a class containing advice methods.
  • Methods will be invoked based on AOP configuration.

For Before / After -TrackOperation.java

Java
public void myadvice(JoinPoint jp) {
    System.out.println("additional concern");
}

For AfterReturning:

Java
public void myadvice(JoinPoint jp, Object result) {
    System.out.println("Result: " + result);
}

For Around:

Java
public Object myadvice(ProceedingJoinPoint pjp) throws Throwable {
    System.out.println("Before method");
    Object obj = pjp.proceed();
    System.out.println("After method");
    return obj;
}

For AfterThrowing:

Java
public void myadvice(JoinPoint jp, Throwable error) {
    System.out.println("Exception: " + error);
}

Step 4: Configure applicationContext.xml

  • Define beans for target class and aspect class
  • Enable AOP using <aop:aspectj-autoproxy/>
  • Configure pointcuts and advice

File: applicationContext.xml

XML
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans/"
    xmlns:xsi="https://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-3.0.xsd 
    http://www.springframework.org/schema/aop/ 
    http://www.springframework.org/schema/aop//spring-aop-3.0.xsd ">

<aop:aspectj-autoproxy />

<bean id="opBean" class="com.Geeksforgeeks.Operation">    </bean>
    
<bean id="trackAspect" class="com.Geeksforgeeks.TrackOperation"></bean>
        
<aop:config>
  <aop:aspect id="myaspect" ref="trackAspect" >
     <!-- @AfterReturning -->
     <aop:pointcut id="pointCutAfterReturning"    expression="execution(* com.Geeksforgeeks.Operation.*(..))" />
     <aop:after-returning method="myadvice" returning="result" pointcut-ref="pointCutAfterReturning" />
  </aop:aspect>
</aop:config>
    
</beans>

Step 5: Configure Each Advice Type

A. aop:before

  • Runs before method execution

xml :<aop:before method="myadvice" pointcut-ref="pointCutBefore"/>

B. aop:after

  • Runs after method execution (always)

xml :<aop:after method="myadvice" pointcut-ref="pointCutAfter"/>

C. aop:after-returning

  • Runs only after successful execution

xml :<aop:after-returning method="myadvice" returning="result"

pointcut-ref="pointCutAfterReturning"/>

D. aop:around

  • Runs before and after method execution

xml : <aop:around method="myadvice" pointcut-ref="pointCutAround"/>

E. aop:after-throwing

  • Runs when exception occurs

xml: <aop:after-throwing method="myadvice" throwing="error"

pointcut-ref="pointCutAfterThrowing"/>

Step 6: Define Pointcut Expression

  • Common pointcut used (xml):

<aop:pointcut id="pointCut"

expression="execution(* com.geeksforgeeks.Operation.*(..))"/>

Step 7: Create Main Class (Test.java)

  • Load Spring container
  • Get bean and call methods
Java
ApplicationContext context =
    new ClassPathXmlApplicationContext("applicationContext.xml");

Operation op = (Operation) context.getBean("opBean");

op.msg();
op.m();
op.k();

Step 8: Run Application

Execute the main class . Observe:

  • Before advice -> executes first
  • After advice -> executes after method
  • Around -> executes before & after
  • AfterReturning -> executes on success
  • AfterThrowing -> executes on exception

output:

Comment