Spring AOP

Spring AOP After and Around Advice Type

Advice:

Action taken by an aspect at a particular join point. Different types of advice include “around,” “before” and “after” advice. Advice types are discussed below. Many AOP frameworks, including Spring, model an advice as an interceptor, maintaining a chain of interceptors “around” the join point.

We’ll learn about the After Advice types: After (finally), AfterReturning and AfterThrowing.

package com.dineshonjava.Spring.Aop;

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

import com.dineshonjava.Spring.Aop.service.ShapeService;

public class AopTest {

    public static void main(String args[])
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ShapeService service=context.getBean("shapeService",ShapeService.class);
        service.getCircle().setNameAndReturning("Dummy Circle return");
    }
}
package com.dineshonjava.Spring.Aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;

import com.dineshonjava.Spring.Aop.model.Circle;

@Aspect
public class LoggingAspect {
   
    public void loggingAdvice(JoinPoint joinpoint)
    {
        System.out.println("Run Advice..... get method Executed");
        System.out.println(joinpoint.toString());
        Circle c=(Circle)joinpoint.getTarget();
        System.out.println(c.getName());
    }
   
    @AfterReturning(pointcut="args(name)",returning="returnString")
    public void allStringMethodArguments(String name, String returnString){
    System.out.println("A setter method has been executed............"+name);
    }
   
    @AfterThrowing(pointcut="args(name)",throwing="ex")
    public void exceptionAdvice(String name, Exception ex){
    System.out.println("Exception is thrown ............"+ex);
    }
   
    @After("args(String)")
    public void afterAdvice(){
    System.out.println("After Advice is executed...........");
    }
}

 

package com.dineshonjava.Spring.Aop;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Setter method is called.....");
        throw(new RuntimeException());
    }
   
    public String setNameAndReturning(String name) {
        this.name = name;
        System.out.println("Setter Method and return the value method executed....");
        return name;
    }
}
package com.dineshonjava.Spring.Aop;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
package com.dineshonjava.Spring.Aop;

import com.dineshonjava.Spring.Aop.model.Circle;
import com.dineshonjava.Spring.Aop.model.Triangle;

public class ShapeService {

    private Triangle triangle;
    private Circle circle;
    public Triangle getTriangle() {
        return triangle;
    }
    public void setTriangle(Triangle triangle) {
        this.triangle = triangle;
    }
    public Circle getCircle() {
        return circle;
    }
    public void setCircle(Circle circle) {
        this.circle = circle;
    }
}
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy>
    <bean class="com.dineshonjava.Spring.Aop.model.Triangle" name="triangle">
    <property name="name" value="triangle name">
    </property></bean>
    <bean class="com.dineshonjava.Spring.Aop.model.Circle" name="circle">
<!--     <property name="name" value="circle name"/>  -->
    </bean>
    <bean class="com.dineshonjava.Spring.Aop.service.ShapeService" name="shapeService">
    <property name="triangle" ref="triangle">
    <property name="circle" ref="circle">
    </property></property></bean>
    <bean class="com.dineshonjava.Spring.Aop.Aspect.LoggingAspect" name="loggingAdvice">
</bean></aop:aspectj-autoproxy></beans>

Around Advice Type

This example covers the Around advice type. We’ll learn how to use it, and we’ll also look at some of the unique and powerful features that are specific to this advice type.
package com.dineshonjava.Spring.Aop;

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

import com.dineshonjava.Spring.Aop.service.ShapeService;

public class AopTest {

    public static void main(String args[])
    {
        ApplicationContext context=new ClassPathXmlApplicationContext("applicationContext.xml");
        ShapeService service=context.getBean("shapeService",ShapeService.class);
        service.getCircle();
    }
}
package com.dineshonjava.Spring.Aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;

import com.dineshonjava.Spring.Aop.model.Circle;

@Aspect
public class LoggingAspect {
   
    public void loggingAdvice(JoinPoint joinpoint)
    {
        System.out.println("Run Advice..... get method Executed");
        System.out.println(joinpoint.toString());
        Circle c=(Circle)joinpoint.getTarget();
        System.out.println(c.getName());
    }
   
    @AfterReturning(pointcut="args(name)",returning="returnString")
    public void allStringMethodArguments(String name, String returnString){
    System.out.println("A setter method has been executed............"+name);
    }
   
    @AfterThrowing(pointcut="args(name)",throwing="ex")
    public void exceptionAdvice(String name, Exception ex){
    System.out.println("Exception is thrown ............"+ex);
    }
   
    @After("args(String)")
    public void afterAdvice(){
    System.out.println("After Advice is executed...........");
    }

    @Pointcut("execution(*  get*())")
    public void allGetters(){}
   
   
    @Around("allGetters()")
    public Object myAroundAdvice(ProceedingJoinPoint proceedingJoinPoint)
    {
        Object returnValue=null;
            try {
                System.out.println("Before method Executed");
                returnValue=proceedingJoinPoint.proceed();
                System.out.println("After Method Executed....");
            } catch (Throwable e) {
                System.out.println("Around Advice throws exception");
        }
        System.out.println("After Finally executed........");
               
        return returnValue;
    }
 }
package com.dineshonjava.Spring.Aop;

public class Circle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        System.out.println("Setter method is called.....");
        throw(new RuntimeException());
    }
   
    public String setNameAndReturning(String name) {
        this.name = name;
        System.out.println("Setter Method and return the value method executed....");
        return name;
    }
}
package com.dineshonjava.Spring.Aop;

public class Triangle {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
<beans xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.0.xsd
                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd">
<aop:aspectj-autoproxy>
    <bean class="com.dineshonjava.Spring.Aop.model.Triangle" name="triangle">
    <property name="name" value="triangle name">
    </property></bean>
    <bean class="com.dineshonjava.Spring.Aop.model.Circle" name="circle">
<!--     <property name="name" value="circle name"/>  -->
    </bean>
    <bean class="com.dineshonjava.Spring.Aop.service.ShapeService" name="shapeService">
    <property name="triangle" ref="triangle">
    <property name="circle" ref="circle">
    </property></property></bean>
    <bean class="com.dineshonjava.Spring.Aop.Aspect.LoggingAspect" name="loggingAdvice">
</bean></aop:aspectj-autoproxy></beans>

 

 

 

Previous
Next
Dinesh Rajput

Dinesh Rajput is the chief editor of a website Dineshonjava, a technical blog dedicated to the Spring and Java technologies. It has a series of articles related to Java technologies. Dinesh has been a Spring enthusiast since 2008 and is a Pivotal Certified Spring Professional, an author of a book Spring 5 Design Pattern, and a blogger. He has more than 10 years of experience with different aspects of Spring and Java design and development. His core expertise lies in the latest version of Spring Framework, Spring Boot, Spring Security, creating REST APIs, Microservice Architecture, Reactive Pattern, Spring AOP, Design Patterns, Struts, Hibernate, Web Services, Spring Batch, Cassandra, MongoDB, and Web Application Design and Architecture. He is currently working as a technology manager at a leading product and web development company. He worked as a developer and tech lead at the Bennett, Coleman & Co. Ltd and was the first developer in his previous company, Paytm. Dinesh is passionate about the latest Java technologies and loves to write technical blogs related to it. He is a very active member of the Java and Spring community on different forums. When it comes to the Spring Framework and Java, Dinesh tops the list!

Share
Published by
Dinesh Rajput

Recent Posts

Strategy Design Patterns using Lambda

Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…

2 years ago

Decorator Pattern using Lambda

Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…

2 years ago

Delegating pattern using lambda

Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…

2 years ago

Spring Vs Django- Know The Difference Between The Two

Technology has emerged a lot in the last decade, and now we have artificial intelligence;…

2 years ago

TOP 20 MongoDB INTERVIEW QUESTIONS 2022

Managing a database is becoming increasingly complex now due to the vast amount of data…

2 years ago

Scheduler @Scheduled Annotation Spring Boot

Overview In this article, we will explore Spring Scheduler how we could use it by…

2 years ago