AOP Introduction and example

In this post, we will learn about AOP (Aspect Oriented Programming). AOP is a way to apply services also called as cross cutting services to objects. In simple words Aspect Oriented Programming (AOP) concept is similar to the triggers concept in data base. Using aspects Spring can add additional functionality to a method execution. This additional functionality could be before a method execution or after method execution. Before we proceed with the example, there are certain concepts in Aspect Oriented Programming which we need to understand.
  • Aspect: Aspect is a modularization on concern that cuts across multiple objects. In Spring AOP, aspects are implemented using regular classes (the schema-based approach) or regular classes annotated with the @Aspect annotation.
  • Join Point: It is a point during the execution of a program. Execution of a method or handling an exception are examples of Join Point.
  • Advice: Advice is the actual action to be taken either before or after the method execution. The method written is invoked by the spring framework during the program execution.
  • Pointcut: It is the point of execution in the application at which cross cutting concern need to be applied. In other words this is a set of one or more join points where an advice should be executed. We will learn more on pointcut in the next post.
  • Introduction: An introduction is also known as inner type declaration, allows you to add new methods or attributes to existing classes.
  • Target object: It is an object being advised by one or more aspects. Also referred to as the advised object. Since Spring AOP is implemented using runtime proxies, this object will always be a proxied object.
  • AOP proxy: It is an object created by the AOP framework in order to implements the aspect contracts.
  • Weaving: Weaving as the word means is to link aspects with other application types or objects to finally create an advised object.
File: com.ram.model.Triangle.java
package com.ram.model;

public class Triangle {
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
}

File: com.ram.model.Circle.java
package com.ram.model;

public class Circle {
 private String name;

 public String getName() {
  return name;
 }

 public void setName(String name) {
  this.name = name;
 }
 
}

File: com.ram.service.ShapeService.java
package com.ram.service;

import com.ram.model.Circle;
import com.ram.model.Triangle;

public class ShapeService {
 private Circle circle;
 private Triangle triangle;
 
 public Circle getCircle() {
  return circle;
 }
 public void setCircle(Circle circle) {
  this.circle = circle;
 }
 public Triangle getTriangle() {
  return triangle;
 }
 public void setTriangle(Triangle triangle) {
  this.triangle = triangle;
 }
 
}

There are different types of advice, but for this example lets take @Before. This advice executes before a join point.
File: com.ram.aspect.LoggingAspect.java
package com.ram.aspect;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Aspect
public class LoggingAspect {
 
 //Advice methods can be configured to run on a particular method.
 @Before("execution(public String getName())")
 public void LoggingAdvice(){
  System.out.println("Advice run. Get method called");
 }
}

File: WEB-INF/dispatcher-servlet.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"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans 
       http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
       http://www.springframework.org/schema/context 
       http://www.springframework.org/schema/context/spring-context-3.0.xsd
       http://www.springframework.org/schema/aop 
       http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
 
    <!-- Tells the spring that we are using aspects in our application -->   
    <aop:aspectj-autoproxy/>
       
 <bean name="triangle" class="com.ram.model.Triangle">
  <property name="name" value="Triangle Name"></property>
 </bean>
 
 <bean name="circle" class="com.ram.model.Circle">
  <property name="name" value="Circle Name"></property>
 </bean>
 
 <bean name="shapeService" class="com.ram.service.ShapeService" autowire="byName"/>
 
 <bean name="loggingAspect" class="com.ram.aspect.LoggingAspect" />
    
</beans>

File: com.ram.main.AOPMain.java
package com.ram.main;

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

import com.ram.service.ShapeService;

public class AOPMain {
 public static void main(String[] args) {
  ApplicationContext context = new ClassPathXmlApplicationContext("spring.xml");
  ShapeService service = context.getBean("shapeService", ShapeService.class);
  System.out.println(service.getCircle().getName());
 }

}

Run the AOPMain.java program and you get the below output:

No comments:

Post a Comment