XML Schema Based AOP With Spring
XML Schema Based AOP With Spring
</beans>
You will also need the following AspectJ libraries on the CLASSPATH of your
application. These libraries are available in the 'lib' directory of an AspectJ
installation, otherwise you can download them from the internet.
aspectjrt.jar
aspectjweaver.jar
aspectj.jar
aopalliance.jar
Declaring an aspect
An aspect is declared using the <aop:aspect> element, and the backing bean is
referenced using the ref attribute as follows −
<aop:config>
<aop:aspect id = "myAspect" ref = "aBean">
...
</aop:aspect>
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 1/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
</aop:config>
Here "aBean" will be configured and dependency injected just like any other Spring
bean as you have seen in the previous chapters.
Declaring a pointcut
A pointcut helps in determining the join points (ie methods) of interest to be
executed with different advices. While working with XML Schema-based
configuration, pointcut will be defined as follows −
<aop:config>
<aop:aspect id = "myAspect" ref = "aBean">
<aop:pointcut id = "businessService"
expression = "execution(*com.xyz.myapp.service.*.*(..))"/>
...
</aop:aspect>
</aop:config>
The following example defines a pointcut named 'businessService' that will match the
execution of getName() method available in the Student class under the package
com.tutorialspoint −
<aop:config>
<aop:aspect id = "myAspect" ref = "aBean">
<aop:pointcut id = "businessService"
expression = "execution(*com.tutorialspoint.Student.getName(..))"/>
...
</aop:aspect>
</aop:config>
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 2/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
...
</bean>
Declaring advices
You can declare any of the five advices inside an <aop:aspect> using the <aop:
{ADVICE NAME}> element as given below −
<aop:config>
<aop:aspect id = "myAspect" ref = "aBean">
<aop:pointcut id = "businessService"
expression = "execution(* com.xyz.myapp.service.*.*(..))"/>
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 3/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
You can use same doRequiredTask or different methods for different advices.
These methods will be defined as a part of aspect module.
Step Description
Add required Spring libraries using Add External JARs option as explained in
2
the Spring Hello World Example chapter.
The final step is to create the content of all the Java files and Bean
6
Configuration file and run the application as explained below.
Here is the content of Logging.java file. This is actually a sample of aspect module
which defines the methods to be called at various points.
package com.tutorialspoint;
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 4/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
/**
* This is the method which I would like to execute
* after a selected method execution.
*/
public void afterAdvice(){
System.out.println("Student profile has been setup.");
}
/**
* This is the method which I would like to execute
* when any method returns.
*/
public void afterReturningAdvice(Object retVal) {
System.out.println("Returning:" + retVal.toString() );
}
/**
* This is the method which I would like to execute
* if there is an exception raised.
*/
public void AfterThrowingAdvice(IllegalArgumentException ex){
System.out.println("There has been an exception: " + ex.toString());
}
}
package com.tutorialspoint;
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 5/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
this.name = name;
}
public String getName() {
System.out.println("Name : " + name );
return name;
}
public void printThrowException(){
System.out.println("Exception raised");
throw new IllegalArgumentException();
}
}
package com.tutorialspoint;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 6/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
<aop:config>
<aop:aspect id = "log" ref = "logging">
<aop:pointcut id = "selectAll"
expression = "execution(* com.tutorialspoint.*.*(..))"/>
</beans>
Once you are done creating the source and bean configuration files, let us run the
application. If everything is fine with your application, it will print the following
message −
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 7/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
The above defined <aop:pointcut> selects all the methods defined under the
package com.tutorialspoint. Let us suppose, you want to execute your advice before
or after a particular method, you can define your pointcut to narrow down your
execution by replacing stars (*) in pointcut definition with the actual class and
method names. Following is a modified XML configuration file to show the concept −
<aop:config>
<aop:aspect id = "log" ref = "logging">
<aop:pointcut id = "selectAll"
expression = "execution(* com.tutorialspoint.Student.getName(..))
<aop:before pointcut-ref = "selectAll" method = "beforeAdvice"/>
<aop:after pointcut-ref = "selectAll" method = "afterAdvice"/>
</aop:aspect>
</aop:config>
</beans>
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 8/9
4/21/24, 9:59 PM XML Schema Based AOP with Spring
If you execute the sample application with these configuration changes, it will print
the following message −
https://www.tutorialspoint.com/spring/schema_based_aop_appoach.htm 9/9