Spring allows you to specify aspects, recommendations, and pointcuts in an XML file. Annotations were used in the aop examples on the preceding page. The XML configuration file will now show us the same samples.
Let’s look at the XML components that describe advice.
- aop:before: It’s used before the real business logic procedure is called.
- aop:after: It is used after the real business logic method has been called.
- aop:after-returning: It is applied after invoking the actual business logic method used. It may be used to intercept the advisory return value.
- aop:around: It is used both before and after the real business logic method is called.
- aop:after-throwing: If the real business logic procedure throws an exception, it is used.
A. aop:before Example
Before the main business logic procedure, the AspectJ Before Advice is used. Any action, such as conversion or authentication, can be performed here. Make a class with real business logic in it.
File: Operation.java
Java
package com.Geeksforgeeks;
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)
{
System.out.println( "additional concern" );
}
}
|
File: applicationContext.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< 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" >
< 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
package com.Geeksforgeeks;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args)
{
ApplicationContext context
= new ClassPathXmlApplicationContext(
"applicationContext.xml" );
Operation e = (Operation)context.getBean( "opBean" );
/ 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" ?>
< 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" >
< aop:pointcut id = "pointCutAfter" expression = "execution(* com.Geeksforgeeks.Operation.*(..))" />
< aop:after method = "myadvice" pointcut-ref = "pointCutAfter" />
</ aop:aspect >
</ aop:config >
</ beans >
|
Output:
You 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 ;}
}
|
File: TrackOperation.java
Java
package com.Geeksforgeeks;
import org.aspectj.lang.JoinPoint;
public class TrackOperation{
public void myadvice(JoinPoint jp,Object result)
{
System.out.println( "additional concern" );
System.out.println( "Method Signature: " + jp.getSignature());
System.out.println( "Result in advice: " +result);
System.out.println( "end of after returning advice..." );
}
}
|
File: applicationContext.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< 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" >
< aop:pointcut id = "pointCutAfterReturning" expression = "execution(* com.Geeksforgeeks.Operation.*(..))" />
< aop:after-returning method = "myadvice" returning = "result" pointcut-ref = "pointCutAfterReturning" />
</ aop:aspect >
</ aop:config >
</ beans >
|
File: Test.java
Java
package com.Geeksforgeeks;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test{
public static void main(String[] args){
ApplicationContext context = new ClassPathXmlApplicationContext( "applicationContext.xml" );
Operation e = (Operation) context.getBean( "opBean" );
System.out.println( "calling m..." );
System.out.println(e.m());
System.out.println( "calling k..." );
System.out.println(e.k());
}
}
|
Output:
D. aop:around Example
Before and after invoking the real business logic methods, the AspectJ surrounding guidance is applied. Make a class with real business logic in it.
File: Operation.java
Java
package com.geeksforgeeks;
public class Operation {
public void msg()
{
System.out.println( "msg() is invoked" );
}
public void display()
{
System.out.println( "display() is invoked" );
}
}
|
Create a class that contains advice as an aspect.
You must supply the PreceedingJoinPoint reference to the advise function so that we may execute the proceed() method to continue the request.
File: TrackOperation.java
Java
package com.geeksforgeeks;
import org.aspectj.lang.ProceedingJoinPoint;
public class TrackOperation {
public Object myadvice(ProceedingJoinPoint pjp)
throws Throwable
{
System.out.println(
"Additional Concern Before calling actual method" );
Object obj = pjp.proceed();
System.out.println(
"Additional Concern After calling actual method" );
return obj;
}
}
|
File: applicationContext.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< 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" >
< aop:pointcut id = "pointCutAround" expression = "execution(* com.geeksforgeeks.Operation.*(..))" />
< aop:around method = "myadvice" pointcut-ref = "pointCutAround" />
</ aop:aspect >
</ aop:config >
</ beans >
|
File: Test.java
Now create the Test class that calls the actual methods.
Java
package com.geeksforgeeks;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args)
{
ApplicationContext context
= new classPathXmlApplicationContext(
"applicationContext.xml" );
Operation op = (Operation)context.getBean( "opBean" );
op.msg();
op.display();
}
}
|
Output:
E. aop:after-throwing example
We may print the exception in the TrackOperation class by utilizing it after throwing advice. Let’s take a look at the AspectJ AfterThrowing tip as an example.
Make a class to hold the business logic.
File: Operation.java
Java
package com.geeksforgeeks;
public class Operation {
public void validate( int age) throws Exception
{
if (age < 18 ) {
throw new ArithmeticException( "Not valid age" );
}
else {
System.out.println( "Thanks for vote" );
}
}
}
|
Create an aspect class that contains advice after it has been thrown.
We must additionally give the Throwable reference here in order to intercept the exception.
File: TrackOperation.java
Java
package com.geeksforgeeks;
import org.aspectj.lang.JoinPoint;
public class TrackOperation {
public void myadvice(JoinPoint jp, Throwable error)
{
System.out.println( "additional concern" );
System.out.println( "Method Signature: "
+ jp.getSignature());
System.out.println( "Exception is: " + error);
System.out.println(
"end of after throwing advice..." );
}
}
|
File: applicationContext.xml
XML
<? xml version = "1.0" encoding = "UTF-8" ?>
< 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" >
< aop:pointcut id = "pointCutAfterThrowing" expression = "execution(* com.geeksforgeeks.Operation.*(..))" />
< aop:after-throwing method = "myadvice" throwing = "error" pointcut-ref = "pointCutAfterThrowing" />
</ aop:aspect >
</ aop:config >
</ beans >
|
File: Test.java
Now create the Test class that calls the actual methods.
Java
package com.geeksforgeeks;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args)
{
ApplicationContext context
= new ClassPathXmlApplicationContext(
"applicationContext.xml" );
Operation op = (Operation)context.getBean( "opBean" );
System.out.println( "calling validate..." );
try {
op.validate( 19 );
}
catch (Exception e) {
System.out.println(e);
}
System.out.println( "calling validate again..." );
try {
op.validate( 11 );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
24 Feb, 2023
Like Article
Save Article