XML Schema Based AOP with Spring
To use the AOP namespace tags described in this section, you need to import the springAOP schema as described
<?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
<!-- bean definition & AOP specific configuration -->
</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
Logging.java
package com.hiraymca;
public class Logging {
/**
* This is the method which I would like to execute
* before a selected method execution.
*/
public void beforeAdvice(){
System.out.println(“Going to setup student profile.”);
}
/**
* 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());
}
Student.java
package com.hiraymca;
public class Student {private Integer age; private String name;
public void setAge(Integer age) {this.age = age;
}
public Integer getAge() {System.out.println(“Age : “ + age);
return age;
}
public void setName(String name) {this.name = name;
}
public String getName() {System.out.println(“Name : “ + name); return name;
}
public void printThrowException(){System.out.println(“Exception raised”);
throw new IllegalArgumentException();
}
}
MainApp.java
package com.hiraymca;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
ApplicationContext context = new
ClassPathXmlApplicationContext(“Beans.xml”);
Student student = (Student) context.getBean(“student”); student.getName();
student.getAge(); student.printThrowException();
}
}
<?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(* com.tutorialspoint.*.*(..))”/>
<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>
<!-- Definition for student bean -->
<bean id = “student” class = “com.tutorialspoint.Student”>
<property name = “name” value = “Zara” />
<property name = “age” value = “11”/>
</bean>
<!-- Definition for logging aspect -->
<bean id = “logging” class = “com.tutorialspoint.Logging”/>
</beans>
Going to setup student profile. Name : Zara
Student profile has been setup. Returning:Zara
Going to setup student profile. Age : 11
Student profile has been setup. Returning:11
Going to setup student profile. Exception raised
Student profile has been setup.
There has been an exception: java.lang.IllegalArgumentException
.....other exception content
Spring AOP – before advice
Write a program to demonstrate Spring AOP – before advice.
Logging.java
package com.hiraymca;
public class Logging {
//Types of Advice
//1 beforeAdvice
{
System.out.println(“Setuping Student Profile “);
}
}
package com.hiraymca;
public class Student {private int age; private String name; public int getAge() {
System.out.println(“Age-”+age);
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getName() {System.out.println(“Name=“+name); return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.hiraymca;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
// TODO Auto-generated method stub
ApplicationContext context=new
ClassPathXmlApplicationContext(“Beans.xml”);
Student s=(Student)context.getBean(“student”); s.getName();
s.getAge();
}
}
<?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” xmlns:c=“http://www.springframework.org/schema/c” xmlns:lang=“http://www.springframework.org/schema/lang” xmlns:util=“http://www.springframework.org/schema/util” xsi:schemaLocation=“http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd”>
<aop:config>
<aop:aspect id=“log” ref=“logging”>
<aop:pointcut expression=“execution(* com.hiraymca.*.*(..))” id=“myid”/>
<aop:before method=“beforeAdvice” pointcut-ref=“myid”/>
</aop:aspect>
</aop:config>
<bean id=“student” class=“com.hiraymca.Student”>
<property name=“name” value=“Sukhiram”></property>
<property name=“age” value=“35”></property>
</bean>
<bean id=“logging” class=“com.hiraymca.Logging”></bean>
</beans>
Spring AOP – after advice
Write a program to demonstrate Spring AOP – after advice.
Logging.java
package com.hiraymca;
public class Logging {
//Types of Advice
//1 beforeAdvice
public void afterAdvice()
{
System.out.println(“Student Profile done “);
}
}
package com.hiraymca;
public class Student {private int age; private String name; public int getAge() {
System.out.println(“Age-”+age);
return age;
}
public void setAge(int age) {
}
public String getName() {System.out.println(“Name=“+name); return name;
}
public void setName(String name) {
this.name = name;
}
}
package com.hiraymca;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class MainApp {
public static void main(String[] args) {
// TODO Auto-generated method stub ApplicationContext context=new
ClassPathXmlApplicationContext(“Beans.xml”);
Student s=(Student)context.getBean(“student”); s.getName();
s.getAge();
}
}
<?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” xmlns:c=“http://www.springframework.org/schema/c” xmlns:lang=“http://www.springframework.org/schema/lang” xmlns:util=“http://www.springframework.org/schema/util” xsi:schemaLocation=“http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/lang
http://www.springframework.org/schema/lang/spring-lang-4.3.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-4.3.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-4.3.xsd”>
<aop:config>
<aop:aspect id=“log” ref=“logging”>
<aop:pointcut expression=“execution(* com.hiraymca.*.*(..))” id=“myid”/>
<aop:after method=“afterAdvice” pointcut-ref=“myid” />
</aop:aspect>
</aop:config>
<bean id=“student” class=“com.hiraymca.Student”>
<property name=“name” value=“Sukhiram”></property>
<property name=“age” value=“35”></property>
</bean>
<bean id=“logging” class=“com.hiraymca.Logging”></bean>
</beans>