Throws Advice Example Spring AOP

Throws Advice Example Spring AOP


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 applicationContext3.xml.

This method executes when any one bean method throws an exception. Now add a new class PleaseThrowsAdvice implementing ThrowsAdvice interface and override the abstract method afterThrowing() method.

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

Bean Program – File Name: Student.java

Add a new class – PleaseThrowsAdvice.java

afterThrowing() is a predefined callback abstract method of interface org.springframework.aop.ThrowsAdvice.

Configuration file: applicationContext3.xml

Client Program: Client4.java

image

Observe, the afterThrowing() method is called after calling all the methods of Student bean. It is called if any one method of Student bean throws an exception.

When to use throws advice (afterThrowing() method?

When some exception is thrown while a method is being executed, to handle the exception properly, throws advice can be used.

Leave a Reply

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