After Aspect XML implementation example

create a spring maven project so that we can manage our dependencies in the pom.xml easily.

Project structure looks like below

AOP_XML_After


Define the pom.xml file as below to include AspectJ jars and spring jars

  1. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  2.   xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  3.   <modelVersion>4.0.0</modelVersion>
  4.  
  5.   <groupId>Spring</groupId>
  6.   <artifactId>SpringAOP</artifactId>
  7.   <version>0.0.1-SNAPSHOT</version>
  8.   <packaging>jar</packaging>
  9.  
  10.   <name>SpringAOP</name>
  11.   <url>http://maven.apache.org</url>
  12.  
  13.   <properties>
  14.     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  15.   </properties>
  16.  
  17.   <dependencies>
  18.   <dependency>
  19.     <groupId>org.springframework</groupId>
  20.     <artifactId>spring-core</artifactId>
  21.     <version>4.2.4.RELEASE</version>
  22. </dependency>
  23.   <dependency>
  24.     <groupId>org.springframework</groupId>
  25.     <artifactId>spring-context</artifactId>
  26.     <version>4.2.4.RELEASE</version>
  27. </dependency>
  28. <dependency>
  29.     <groupId>org.aspectj</groupId>
  30.     <artifactId>aspectjrt</artifactId>
  31.     <version>1.7.3</version>
  32. </dependency>
  33.  
  34. <dependency>
  35.     <groupId>aspectj</groupId>
  36.     <artifactId>aspectjweaver</artifactId>
  37.     <version>1.5.4</version>
  38. </dependency>
  39.  
  40.  
  41.     <dependency>
  42.       <groupId>junit</groupId>
  43.       <artifactId>junit</artifactId>
  44.       <version>3.8.1</version>
  45.       <scope>test</scope>
  46.     </dependency>
  47.   </dependencies>
  48. </project>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>

  <groupId>Spring</groupId>
  <artifactId>SpringAOP</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>jar</packaging>

  <name>SpringAOP</name>
  <url>http://maven.apache.org</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  </properties>

  <dependencies>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-core</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
  <dependency>
	<groupId>org.springframework</groupId>
	<artifactId>spring-context</artifactId>
	<version>4.2.4.RELEASE</version>
</dependency>
<dependency>
	<groupId>org.aspectj</groupId>
	<artifactId>aspectjrt</artifactId>
	<version>1.7.3</version>
</dependency>

<dependency>
	<groupId>aspectj</groupId>
	<artifactId>aspectjweaver</artifactId>
	<version>1.5.4</version>
</dependency>


    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
</project>


Let’s create a class which contains a business logic on which we apply Advice.

  1. package com.kb;
  2. public class SimpleCalculator {
  3.     public int add(int x,int y){
  4.         System.out.println("inside Add");
  5.         return x+y;
  6.     }
  7.     public int subtract(int x,int y){
  8.         System.out.println("inside subtract");
  9.         return x-y;
  10.     }
  11. }
package com.kb;
public class SimpleCalculator {
	public int add(int x,int y){
		System.out.println("inside Add");
		return x+y;
	}
	public int subtract(int x,int y){
		System.out.println("inside subtract");
		return x-y;
	}
}

This class has 2 methods where each method performs some business logic,which is addition and subtraction.



Now let’s write the Aspect class as below

  1. package com.kb;
  2. import org.aspectj.lang.JoinPoint;
  3.  
  4. public class MyAfterAspect {
  5.    
  6.     public void myAdvice(JoinPoint jp){
  7.         System.out.println("After the method "+jp.getSignature().getName()+" completes the execution");
  8.     }
  9. }
package com.kb;
import org.aspectj.lang.JoinPoint;

public class MyAfterAspect {
	
	public void myAdvice(JoinPoint jp){
		System.out.println("After the method "+jp.getSignature().getName()+" completes the execution");
	}
}

This Aspect has an advice called myAdvice whose action is to print the log with method name


Now let’s create a beans.xml file which is the spring configuration file where we define our aspect beans and configure the AOP as below

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7.        http://www.springframework.org/schema/aop  
  8.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  9.        http://www.springframework.org/schema/context
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  11.  
  12.     <aop:aspectj-autoproxy />
  13.  
  14.     <bean id="simpleCalc" class="com.kb.SimpleCalculator"/>
  15.  
  16.     <bean id="myAfterAspect" class="com.kb.MyAfterAspect" />
  17.  
  18.     <aop:config>
  19.         <aop:aspect id="myAspect" ref="myAfterAspect">
  20.             <aop:pointcut id="pointCutAfter"
  21.                 expression="execution(* com.kb.SimpleCalculator.*(..))" />
  22.             <aop:after method="myAdvice" pointcut-ref="pointCutAfter" />
  23.         </aop:aspect>
  24.     </aop:config>
  25.  
  26. </beans>
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop   
    	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/context
  		http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<aop:aspectj-autoproxy />

	<bean id="simpleCalc" class="com.kb.SimpleCalculator"/>

	<bean id="myAfterAspect" class="com.kb.MyAfterAspect" />

	<aop:config>
		<aop:aspect id="myAspect" ref="myAfterAspect">
			<aop:pointcut id="pointCutAfter"
				expression="execution(* com.kb.SimpleCalculator.*(..))" />
			<aop:after method="myAdvice" pointcut-ref="pointCutAfter" />
		</aop:aspect>
	</aop:config>

</beans>


We have to add below elements in spring xml file to configure AOP

Need to define AOP namespacelike xmlns:aop=http://www.springframework.org/schema/aop

Need to add < aop:aspectj-autoproxy /> to enable Spring AspectJ support with auto proxy at runtime

Need to configure Aspect classes as any other spring beans

Need to define aop configuration which defines the pointcut and advice mapping using < aop:config> element

expression=”execution(* com.kb.SimpleCalculator.*(..))” defines that advice must be applied to all the methods inside SimpleCalculator class no matter what is its return type and its parameters.



Lets write a class which calls the actual business logic method as below

  1. package com.kb;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4.  
  5. public class AOPTest {
  6.     public static void main(String[] args) {
  7.     ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");  
  8.        SimpleCalculator calculator = (SimpleCalculator) context.getBean("simpleCalc");
  9.       int res1= calculator.add(10, 5);
  10.       System.out.println("Addition result is "+res1);
  11.       int res2= calculator.subtract(10, 5);
  12.       System.out.println("Subtracted result is "+res2);
  13.     }
  14. }
package com.kb;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTest {
	public static void main(String[] args) {
	ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");  
       SimpleCalculator calculator = (SimpleCalculator) context.getBean("simpleCalc"); 
      int res1= calculator.add(10, 5);
      System.out.println("Addition result is "+res1);
      int res2= calculator.subtract(10, 5);
      System.out.println("Subtracted result is "+res2);
	}
}


Run the above class and see the output as below

Observe the output, as soon as the method add and subtract completes the execution,our advice code has been executed.

Note: AOP After advice will always get executed no matter whether we get method execution successfully or we get any exception.


Now create a class which is a copy of SimpleCalculator but it throws exception inside the method instead of executing normally.

  1. package com.kb;
  2. public class SimpleCalculatorThrowsException {
  3.                public int add(int x,int y){
  4.         System.out.println("inside Add");
  5.         throw new RuntimeException();
  6.     }
  7. }
package com.kb;
public class SimpleCalculatorThrowsException {
               public int add(int x,int y){
		System.out.println("inside Add");
		throw new RuntimeException();
	}
}


Now modify the beans.xml file to define the above bean

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
  5.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  6.        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
  7.        http://www.springframework.org/schema/aop  
  8.         http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
  9.        http://www.springframework.org/schema/context
  10.         http://www.springframework.org/schema/context/spring-context-3.2.xsd">
  11.  
  12.     <aop:aspectj-autoproxy />
  13.  
  14.     <bean id="simpleCalc" class="com.kb.SimpleCalculator"/>
  15.    
  16.     <bean id="simpleCalcException" class="com.kb.SimpleCalculatorThrowsException"/>
  17.  
  18.     <bean id="myAfterAspect" class="com.kb.MyAfterAspect" />
  19.  
  20.     <aop:config>
  21.         <aop:aspect id="myAspect" ref="myAfterAspect">
  22.             <aop:pointcut id="pointCutAfter"
  23.                 expression="execution(* *.*(..))" />
  24.             <aop:after method="myAdvice" pointcut-ref="pointCutAfter" />
  25.         </aop:aspect>
  26.     </aop:config>
  27.  
  28. </beans>
<?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:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/aop   
    	http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
        http://www.springframework.org/schema/context
  		http://www.springframework.org/schema/context/spring-context-3.2.xsd">

	<aop:aspectj-autoproxy />

	<bean id="simpleCalc" class="com.kb.SimpleCalculator"/>
	
	<bean id="simpleCalcException" class="com.kb.SimpleCalculatorThrowsException"/>

	<bean id="myAfterAspect" class="com.kb.MyAfterAspect" />

	<aop:config>
		<aop:aspect id="myAspect" ref="myAfterAspect">
			<aop:pointcut id="pointCutAfter"
				expression="execution(* *.*(..))" />
			<aop:after method="myAdvice" pointcut-ref="pointCutAfter" />
		</aop:aspect>
	</aop:config>

</beans>

Note:pointcut expression execution(* *.*(..)) means advice has to be executed for all the classes and all the methods.


Let’s test it using below program

  1. package com.kb;
  2.  
  3. import org.springframework.context.ApplicationContext;
  4. import org.springframework.context.support.ClassPathXmlApplicationContext;
  5.  
  6. public class AOPTestException {
  7.     public static void main(String[] args) {
  8.         ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");  
  9.         SimpleCalculatorThrowsException calculator = (SimpleCalculatorThrowsException) context.getBean("simpleCalcException");
  10.       int res1= calculator.add(10, 5);
  11.       System.out.println("addition result is "+res1);
  12.      
  13.     }
  14. }
package com.kb;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class AOPTestException {
	public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");  
		SimpleCalculatorThrowsException calculator = (SimpleCalculatorThrowsException) context.getBean("simpleCalcException"); 
      int res1= calculator.add(10, 5);
      System.out.println("addition result is "+res1);
     
	}
}


Run the above program and see the output


So observe the output, we can see that advice gets executed even though method throws an exception.

Download this project SpringAOPXML_AfterAdvice.zip

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on