AOP : Aspect Oriented Programming

AOP : programming paradigm that aims to increase modularity by allowing the separation of cross-cutting concerns.

In any Application development we will have 2 things

1)Business Logic
2)Cross cutting logic
1) : Actual business related code : ex adding an employee details to DB
2): cross-cutting concerns are aspects of a program that affect other concerns. Ex: adding logging to the method execution

Lets create a class ManageEmployees

  1. package com.kb.AOPNeed;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class ManageEmployees {
  5.      void addEmployee(long empId){
  6.         System.out.println("addEmployee is running");//DAO call to persist Employee into DB }
  7.      void removeEmployee(long empId){
  8.         System.out.println("removeEmployee is running");//DAO call to delete Employee from DB   }
  9.  
  10.     public static void main(String[] args) {
  11.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/AOPNeed/beans.xml");
  12.     ManageEmployees manageEmployees = applicationContext.getBean("manageEmployees",ManageEmployees.class);
  13.         manageEmployees.addEmployee(100);
  14.         manageEmployees.removeEmployee(100);
  15.     }
  16. }
package com.kb.AOPNeed;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ManageEmployees {
	 void addEmployee(long empId){
		System.out.println("addEmployee is running");//DAO call to persist Employee into DB	}
	 void removeEmployee(long empId){
		System.out.println("removeEmployee is running");//DAO call to delete Employee from DB	}

	public static void main(String[] args) {
		ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/AOPNeed/beans.xml");
	ManageEmployees manageEmployees = applicationContext.getBean("manageEmployees",ManageEmployees.class);
		manageEmployees.addEmployee(100);
		manageEmployees.removeEmployee(100);
	}
}

Now in the above class we have 2 methods addEmployee() and removeEmployee()
Each method is performing its own logic( business logic).

Now if I want to track the entry and exit of these 2 methods , what can we do
Possibly we can write log statements at the beginning and end of those 2 methods which is as below

  1. package com.kb.AOPNeed;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class ManageEmployees {
  5.      void addEmployee(long empId){
  6.          System.out.println("addEmployee() begin");
  7.         System.out.println("addEmployee is running");//DAO call to persist Employee into DB
  8.          System.out.println("addEmployee() end");
  9.     }
  10.      void removeEmployee(long empId){
  11.          System.out.println("removeEmployee() begin");
  12.      System.out.println("removeEmployee is running");//DAO call to delete Employee from DB
  13.          System.out.println("removeEmployee() begin");
  14.     }
  15.  
  16.     public static void main(String[] args) {
  17. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/AOPNeed/beans.xml");
  18.         ManageEmployees manageEmployees = applicationContext.getBean("manageEmployees",ManageEmployees.class);
  19.         manageEmployees.addEmployee(100);
  20.         manageEmployees.removeEmployee(100);
  21.     }
  22. }
package com.kb.AOPNeed;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ManageEmployees {
	 void addEmployee(long empId){
		 System.out.println("addEmployee() begin");
		System.out.println("addEmployee is running");//DAO call to persist Employee into DB
		 System.out.println("addEmployee() end");
	}
	 void removeEmployee(long empId){
		 System.out.println("removeEmployee() begin");
	 System.out.println("removeEmployee is running");//DAO call to delete Employee from DB
		 System.out.println("removeEmployee() begin");
	}

	public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/AOPNeed/beans.xml");
		ManageEmployees manageEmployees = applicationContext.getBean("manageEmployees",ManageEmployees.class);
		manageEmployees.addEmployee(100);
		manageEmployees.removeEmployee(100);
	}
}

Now the output is

see the output , before and after performing business logic, our method have executed the logs which is as expected since it’s a method body.

Assume if we have 1000 such methods in our application then we need to add log message in all the 1000 methods both at the beginning and at the end of the method.
So this kind of adding cross cutting concerns to all other concerns(business logic) can be achieved very well using AOP.

Lets resolve the same above problem using AOP.
We will use Spring supported AspectJ style AOP.
To use AOP with Spring , we need to add following jars to the classpath
aspectjrt.jar aspectjweaver.jar aopalliance-1.0.jar spring-aop.jar

Redefine same ManageEmployees class by removing log messages

  1. package com.kb.AOPNeed;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class ManageEmployees {
  5.      void addEmployee(long empId){
  6.         System.out.println("addEmployee is running");//DAO call to persist Employee into DB
  7.             }
  8.      void removeEmployee(long empId){
  9.      System.out.println("removeEmployee is running");//DAO call to delete Employee from DB 
  10.     }
  11.  
  12.     public static void main(String[] args) {
  13. ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/AOPNeed/beans.xml");
  14.         ManageEmployees manageEmployees = applicationContext.getBean("manageEmployees",ManageEmployees.class);
  15.         manageEmployees.addEmployee(100);
  16.         manageEmployees.removeEmployee(100);
  17.     }
  18. }
package com.kb.AOPNeed;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class ManageEmployees {
	 void addEmployee(long empId){
		System.out.println("addEmployee is running");//DAO call to persist Employee into DB
			}
	 void removeEmployee(long empId){
	 System.out.println("removeEmployee is running");//DAO call to delete Employee from DB	
	}

	public static void main(String[] args) {
ApplicationContext applicationContext = new ClassPathXmlApplicationContext("com/kb/AOPNeed/beans.xml");
		ManageEmployees manageEmployees = applicationContext.getBean("manageEmployees",ManageEmployees.class);
		manageEmployees.addEmployee(100);
		manageEmployees.removeEmployee(100);
	}
}

Now create LoggingAspect using AspectJ style of AOP as below

  1. package com.kb.AOPNeed;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.After;
  4. import org.aspectj.lang.annotation.Aspect;
  5. import org.aspectj.lang.annotation.Before;
  6. @Aspect
  7. public class LoggingAspect {
  8.     @Before("execution(* com.kb.AOPNeed.*.*(..))")
  9.     public void logBeforeExecutingMethod(JoinPoint joinPoint){
  10.         System.out.println("logBeforeExecutingMethods is running  for the method "+joinPoint.getSignature().getName());
  11.        
  12.     }
  13.     @After("execution(* com.kb.AOPNeed.*.*(..))")
  14.     public void logAfterExecutingMethod(JoinPoint joinPoint){
  15.         System.out.println("logAfterExecutingMethods is running  for the method "+joinPoint.getSignature().getName());
  16.        
  17.     }
  18. }
package com.kb.AOPNeed;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LoggingAspect {
	@Before("execution(* com.kb.AOPNeed.*.*(..))")
	public void logBeforeExecutingMethod(JoinPoint joinPoint){
		System.out.println("logBeforeExecutingMethods is running  for the method "+joinPoint.getSignature().getName());
		
	}
	@After("execution(* com.kb.AOPNeed.*.*(..))")
	public void logAfterExecutingMethod(JoinPoint joinPoint){
		System.out.println("logAfterExecutingMethods is running  for the method "+joinPoint.getSignature().getName());
		
	}
}

Create a bean configuration file beans.xml

  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3.     xmlns:aop="http://www.springframework.org/schema/aop"
  4.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  5.     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  6.     http://www.springframework.org/schema/aop
  7.     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd ">
  8.  
  9.     <aop:aspectj-autoproxy />
  10.  
  11.     <bean id="manageEmployees" class="com.kb.AOPNeed.ManageEmployees" />
  12.  
  13.     <!-- Aspect -->
  14.     <bean id="logAspect" class="com.kb.AOPNeed.LoggingAspect" />
  15.  
  16. </beans>
<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:aspectj-autoproxy />
 
	<bean id="manageEmployees" class="com.kb.AOPNeed.ManageEmployees" />
 
	<!-- Aspect -->
	<bean id="logAspect" class="com.kb.AOPNeed.LoggingAspect" />
 
</beans>

Now run the ManageEmployees class defined above

Output is

since we have defined a pointcut to match any method in the ManageEmployees class, our aspect is applied to any number of methods we define inside this class.
So cross cutting concerns is completely separated from the actual business logic.

Lets try to understand terms in AOP

1)Aspect – cross cutting concern ,something global feature of the application.
In our example -> it is LoggingAspect
2)Join Point – point during the execution of the program such as method execution or exception handling code. In simple words the point where the cross cutting concerns can be applied
In our example -> 2 methods addEmployee() and removeEmployee()
3)Advice – the Action taken at the join point. means implementation of any global feature/cross cutting feature.
Printing log message before and after method execution
4)Point cut – Expression which matches the exact join points, it will have many join points in it.
In our example -> @Before(“execution(* com.kb.AOPNeed.*.*(..))”) and @After(“execution(* com.kb.AOPNeed.*.*(..))”) which matches any method inside our ManageEmployees class

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