VOOZH about

URL: https://www.geeksforgeeks.org/java/spring-aop-aspectj-xml-configuration/

⇱ Spring - AOP AspectJ Xml Configuration - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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).
👁 Image
 

Step 2: Create Business Class

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

Operation.java

File: TrackOperation.java

File: applicationContext.xml

File: Test.java

Output:

👁 Image

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

Output:

👁 Image

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

C. aop:after-returning example

👁 Image

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

File: Operation.java

Step 3: Create Aspect Class

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

For Before / After -TrackOperation.java

For AfterReturning:

For Around:

For AfterThrowing:

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

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

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:

👁 Image
Comment
Article Tags:
Article Tags: