After Returning Advice Example Spring AOP

After Returning Advice Example Spring AOP


After Returning Advise

The same bean program of AOP Advices – Types – Tutorial is used. Change comes in configuration XML file and also slightly in client program. Let us name the modified XML file as applicationContext2.xml.

The method afterReturning() executes after each bean method is returned with a result. Add a new class that implements AfterReturningAdvice interface.

1. The bean program used in AOP Advices – Types – Tutorial is used and reproduced here.

Now, the example using After Returning

Bean Program – File Name: Student.java

Add a new class – PleaseCallAfterMethod.java

afterReturning() is a predefined abstract callback method of interface org.springframework.aop.AfterReturningAdvice.

Configuration file: applicationContext2.xml

Client Program: Client3.java


After Returning
Output screenshot on After Returning Example

Observe, after executing each method of bean Student, the method afterReturning() is called.

When to use after advice (afterReturning() method):

After advice is useful when some code is to be executed after the successful execution of the business logic (successful means the business logic method should not throw any exception during its execution). For example, in our code, the best appropriate place to use after advice is cleanUp() method. Here, after advice can execute some clean up code like closing the sessions, JDBC connections, network connections etc.

Pass your comments of how far it is easy to you to understand the concept "After Returning".

2 thoughts on “After Returning Advice Example Spring AOP

  1. Rajeswari K

    Sir,

    How would you specifically make the advice to execute for methods. Say,If I want to execute the before advice only for printName() method and not for other methods int he student class.

    Thanks
    Rajeswari K

  2. Rajeswari K

    Sir,

    How would you specifically make the advice to execute for methods.
    Say,If I want to execute the before advice only for printName() method and not for other methods int he student class.

    Thanks
    Rajeswari K

Leave a Reply

Your email address will not be published. Required fields are marked *