Back to: Spring Framework Tutorials
Spring Framework XML-Based AOP
In this article, I am going to discuss Spring Framework XML-Based AOP with Examples. Please read our previous article, where we discussed Spring Framework AOP.
What is XML-Based AOP in Spring Framework?
XML-based Aspect-Oriented Programming (AOP) in Spring Framework is an approach that allows developers to define aspects, cross-cutting concerns, and their associated advice in XML configuration files. It is a way to modularize certain functionalities that are applicable across different parts of an application, such as logging, security, or transaction management.
In XML-based AOP, aspects are defined separately from the application’s business logic. Aspects are encapsulated in advice, which represents the behavior to be executed at specific points of the application, known as join points. Join points can be method invocations, object creation, or even exceptions being thrown.
XML-based AOP configuration typically consists of three main elements: aspects, pointcuts, and advice. Aspects define the cross-cutting concern, pointcuts specify the join points where the aspect should be applied, and advice describes the behavior to be executed at those join points.
There are different types of advice in XML-based AOP, such as “before,” which executes before a join point, “after,” which executes after a join point (regardless of its success or failure), and “around,” which wraps around a join point, allowing the advice to control the method invocation.
The advantages of XML-based AOP in Spring include separation of concerns, increased modularity, and improved maintainability. By separating cross-cutting concerns from the application logic, developers can focus on implementing business functionality without cluttering the code with repetitive or non-core concerns. Additionally, aspects can be easily configured, enabled, or disabled through XML configuration files without modifying the application’s source code.
However, it’s worth mentioning that XML-based AOP in Spring has become less popular in recent years, as Spring Framework has shifted towards using Java annotations for AOP configuration, which provides a more concise and readable approach.
Steps to use XML Schema-based AOP in Spring Framework
To use XML Schema-based AOP in Spring Framework, you typically follow these steps:
- Define the XML schema: Start by defining an XML schema file that defines the structure and elements for configuring AOP aspects and advice. Spring provides a standard schema, “spring-aop.xsd,” for this purpose.
- Configure AOP aspects: In your XML configuration file, you define aspects using the “aop:config” element. Inside this element, you can define pointcuts, advice, and associations.
- Define pointcuts: Pointcuts define the join points where advice will be applied. You can use the “aop:pointcut” element to define a pointcut and specify the expression or pattern that matches the join points.
- Define advice: Advice specifies the behavior to be applied at a particular join point. Using the “aop:advisor” element, you can define advice types such as “before,” “after,” “around,” and so on. You can also reference pointcuts to associate them with specific advice.
- Apply aspects: Finally, you apply the aspects to your Spring beans using the “aop:aspect” element. Inside this element, you specify the advice or advice references to be applied to the target bean or beans.
By configuring AOP aspects using XML Schema-based approach, we can achieve a separation of concerns and easily manage the cross-cutting behavior of applications without directly modifying the source code.
Implementing XML-Based AOP in Spring Framework
Before starting the project, ensure that the required JAR files are in the project’s “Referenced Libraries” folder.
Step 1: Create a new file called Logging.java in the src/ directory. Add the following contents to the file:
public class Logging { public void beforeAdvice() {System.out.println("Setting up employee profile.");} public void afterAdvice() {System.out.println("Employee profile has been set up.");} public void afterReturningAdvice(Object returnValue) {System.out.println("Returning: " + returnValue.toString());} public void AfterThrowingAdvice (IllegalArgumentException e) {System.out.println("Exception: " + e.toString());} }
These functions will be called before, after, and after throwing advice.
Step 2: Create a new file called Employee.java in the src/ directory. Add the following contents to the file:
public class Employee { private int age; private String name; public int getAge() { return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } @Override public String toString() { return "Employee [name=" + name + " age=" + age + "]"; } public void printThrowException() { System.out.println("Exception raised!"); } }
This class is a POJO class that will be used to store employees.
Step 3: In the src/there must be a file called App.java. This file contains the main function and was created when the project was created (by VS Code). Modify the file as follows:
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class App { public static void main(String[] args) { ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml"); Employee e = (Employee) context.getBean("employee"); System.out.println(e.toString()); e.printThrowException(); } }
The main function creates an employee bean from the Beans.xml file and prints it. It then throws an exception (on purpose) to demonstrate the AOP implemented in the program.
Step 4: Create a new file called Beans.xml in the src/ directory. Add the following contents to the file:
<?xml version = "1.0" encoding = "UTF-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns:xsi = "http://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:config> <aop:aspect id = "log" ref = "logging"> <aop:pointcut id = "selectAll" expression = "execution(* Employee.*(..))" /> <aop:before pointcut-ref = "selectAll" method = "beforeAdvice"/> <aop:after pointcut-ref = "selectAll" method = "afterAdvice"/> <aop:after-returning pointcut-ref = "selectAll" returning = "retVal" method = "afterReturningAdvice"/> <aop:after-throwing pointcut-ref = "selectAll" throwing = "ex" method = "AfterThrowingAdvice"/> </aop:aspect> </aop:config> <bean id = "employee" class = "Employee"> <property name = "name" value = "Employee A" /> <property name = "age" value = "25"/> </bean> <!-- Definition for logging aspect --> <bean id = "logging" class = "Logging"/> </beans>
Step 5: Compile and execute the application. Ensure compilation is successful. Ensure that the output is as expected:
As can be seen, all the required statements execute (before advice, after advice, etc.)
Congratulations! You have completed the XML-based AOP application in Spring Framework!
In the next article, I am going to discuss Spring Framework AspectJ -Based AOP. Here, in this article, I try to explain Spring Framework XML-Based AOP with Examples. I hope you enjoy this Spring Framework XML-Based AOP article.