Spring AOP AspectJ Xml Configuration (original) (raw)

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.

Why Use XML-Based AOP?

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

Step 2: Create Business Class

**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 `

<aop:aspectj-autoproxy />

aop:config <aop:aspect id="myaspect" ref="trackAspect" > <aop:pointcut id="pointCutBefore" expression="execution(* com.Geeksforgeeks.Operation.*(..))" /> <aop:before method="myadvice" pointcut-ref="pointCutBefore" />

`

**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 `

<aop:aspectj-autoproxy />


aop:config
<aop:aspect id="myaspect" ref="trackAspect" >

<aop:pointcut id="pointCutAfter" expression="execution(* com.Geeksforgeeks.Operation.*(..))" />
<aop:after method="myadvice" pointcut-ref="pointCutAfter" />

`

**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

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

**File: applicationContext.xml

XML `

<aop:aspectj-autoproxy />

aop:config <aop:aspect id="myaspect" ref="trackAspect" > <aop:pointcut id="pointCutAfterReturning" expression="execution(* com.Geeksforgeeks.Operation.*(..))" /> <aop:after-returning method="myadvice" returning="result" pointcut-ref="pointCutAfterReturning" />

`

Step 5: Configure Each Advice Type

A. aop:before

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

**B. aop:after

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

**C. aop:after-returning

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

pointcut-ref="pointCutAfterReturning"/>

**D. aop:around

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

**E. aop:after-throwing

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

pointcut-ref="pointCutAfterThrowing"/>

Step 6: Define Pointcut Expression

<aop:pointcut id="pointCut"

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

Step 7: Create Main Class (Test.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:

output: