AspectJ libraries provides the facility to declare aspect, pointcut etc using xml file. Let us discuss the syntax first.
Declaring an aspect:
The
Declaring a pointcut:
The
Declaring advices:
The
Spring AOP AspectJ Xml Configuration Before Advice Example:
BusinessLogic.java
/** * This class will be used as a bean class. * @author w3schools */ public class BusinessLogic { public void implementBusinessLogic(){ System.out.println("Business logic executed."); } } |
BeforeAdviceTest.java
import java.lang.reflect.Method; import org.springframework.aop.MethodBeforeAdvice; public class BeforeAdviceTest implements MethodBeforeAdvice{ @Override public void before(Method method, Object[] args, Object target)throws Throwable { System.out.println("Additional concern " + "before business logic."); } } |
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="businessLogic" class="com.w3schools.business.BusinessLogic"/> <bean id="beforeAdviceTest" class="com.w3schools.business.BeforeAdviceTest"/> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="businessLogic"></property> <property name="interceptorNames"> <list> <value>beforeAdviceTest</value> </list> </property> </bean> </beans> |
Test.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //Get ApplicationContext using spring configuration file. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //Get BusinessLogic bean object from ApplicationContext instance. BusinessLogic businessLogic = (BusinessLogic) context.getBean("proxy", BusinessLogic.class); //Call implementBusinessLogic method of BusinessLogic bean. businessLogic.implementBusinessLogic(); } } |
Output:
Additional concern before business logic.
Business logic executed. |
Spring AOP AspectJ Xml Configuration after-returning Advice Example:
BusinessLogic.java
/** * This class will be used as a bean class. * @author w3schools */ public class BusinessLogic { public void implementBusinessLogic(){ System.out.println("Business logic executed."); } } |
AfterReturningAdviceTest.java
import java.lang.reflect.Method; import org.springframework.aop.AfterReturningAdvice; public class AfterReturningAdviceTest implements AfterReturningAdvice{ @Override public void afterReturning(Object returnValue, Method method, Object[] args, Object target)throws Throwable { System.out.println("Additional concern " + "after returning business logic."); } } |
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="businessLogic" class="com.w3schools.business.BusinessLogic"/> <bean id="afterReturningAdviceTest" class="com.w3schools.business.AfterReturningAdviceTest"/> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="businessLogic"></property> <property name="interceptorNames"> <list> <value>afterReturningAdviceTest</value> </list> </property> </bean> </beans> |
Test.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //Get ApplicationContext using spring configuration file. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //Get BusinessLogic bean object from ApplicationContext instance. BusinessLogic businessLogic = (BusinessLogic) context.getBean("proxy", BusinessLogic.class); //Call implementBusinessLogic method of BusinessLogic bean. businessLogic.implementBusinessLogic(); } } |
Output:
Business logic executed.
Additional concern after returning business logic. |
Spring AOP AspectJ Xml Configuration after-throwing Advice Example:
BusinessLogic.java
/** * This class will be used as a bean class. * @author w3schools */ public class BusinessLogic { public void implementBusinessLogic(int num){ if(num == 0){ throw new ArithmeticException("Exception occures."); } else{ System.out.println("Business logic executed."); } } } |
AfterThrowingAdviceTest.java
import java.lang.reflect.Method; import org.springframework.aop.ThrowsAdvice; public class AfterThrowingAdviceTest implements ThrowsAdvice{ public void afterThrowing(Method m,Object args[], Object target,Exception e) { System.out.println("Additional concern after" + " throwing exception in business logic."); } } |
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="businessLogic" class="com.w3schools.business.BusinessLogic"/> <bean id="afterThrowingAdviceTest" class="com.w3schools.business.AfterThrowingAdviceTest"/> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="businessLogic"></property> <property name="interceptorNames"> <list> <value>afterThrowingAdviceTest</value> </list> </property> </bean> </beans> |
Test.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //Get ApplicationContext using spring configuration file. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //Get BusinessLogic bean object from ApplicationContext instance. BusinessLogic businessLogic = (BusinessLogic) context.getBean("proxy", BusinessLogic.class); //Call implementBusinessLogic method of BusinessLogic bean. businessLogic.implementBusinessLogic(0); } } |
Output:
Additional concern after throwing exception in business logic. Exception in thread "main" java.lang.ArithmeticException: Exception occures. at com.w3schools.business.BusinessLogic.implementBusinessLogic(BusinessLogic.java:10) at com.w3schools.business.BusinessLogic$$FastClassByCGLIB$$19ab96db.invoke(<generated>) at net.sf.cglib.proxy.MethodProxy.invoke(MethodProxy.java:191) at org.springframework.aop.framework.Cglib2AopProxy$CglibMethodInvocation.invokeJoinpoint(Cglib2AopProxy.java:692) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:150) at org.springframework.aop.framework.adapter.ThrowsAdviceInterceptor.invoke(ThrowsAdviceInterceptor.java:124) at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:172) at org.springframework.aop.framework.Cglib2AopProxy$DynamicAdvisedInterceptor.intercept(Cglib2AopProxy.java:625) at com.w3schools.business.BusinessLogic$$EnhancerByCGLIB$$b559ac2d.implementBusinessLogic(<generated>) at com.w3schools.business.Test.main(Test.java:17) |
Spring AOP AspectJ Xml Configuration Around Advice Example:
BusinessLogic.java
/** * This class will be used as a bean class. * @author w3schools */ public class BusinessLogic { public void implementBusinessLogic(){ System.out.println("Business logic executed."); } } |
AroundAdviceTest.java
import org.aopalliance.intercept.MethodInterceptor; import org.aopalliance.intercept.MethodInvocation; public class AroundAdviceTest implements MethodInterceptor{ @Override public Object invoke(MethodInvocation methodInvocation) throws Throwable { Object obj; System.out.println("Additional concern before business logic."); obj=methodInvocation.proceed(); System.out.println("Additional concern after business logic."); return obj; } } |
applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"> <bean id="businessLogic" class="com.w3schools.business.BusinessLogic"/> <bean id="aroundAdviceTest" class="com.w3schools.business.AroundAdviceTest"/> <bean id="proxy" class="org.springframework.aop.framework.ProxyFactoryBean"> <property name="target" ref="businessLogic"></property> <property name="interceptorNames"> <list> <value>aroundAdviceTest</value> </list> </property> </bean> </beans> |
Test.java
import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class Test { public static void main(String[] args) { //Get ApplicationContext using spring configuration file. ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); //Get BusinessLogic bean object from ApplicationContext instance. BusinessLogic businessLogic = (BusinessLogic) context.getBean("proxy", BusinessLogic.class); //Call implementBusinessLogic method of BusinessLogic bean. businessLogic.implementBusinessLogic(); } } |
Output:
Additional concern before business logic. Business logic executed. Additional concern after business logic. |