![]() |
VOOZH | about |
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.
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.
Operation.java
File: TrackOperation.java
File: applicationContext.xml
File: Test.java
Output:
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:
We can see that additional concern is printed after calling msg(), m(), and k() methods.
We may receive the outcome in the advice by using after returning advice. Make a class to hold the business logic.
File: Operation.java
For Before / After -TrackOperation.java
For AfterReturning:
For Around:
For AfterThrowing:
File: applicationContext.xml
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"/>
<aop:pointcut id="pointCut"
expression="execution(* com.geeksforgeeks.Operation.*(..))"/>
Execute the main class . Observe: