You can view the tutorials best in Google Chrome, Mozilla Firefox, Opera, higher version of Internet Explorer

Spring AOP Throws Advice Example With Complete Explanation

Spring » On Jul 28, 2012 By Sivateja

Will see how to work with Throws Advice in spring AOP.

  • In this type of Advice, we implement services which are executed when ever the business logic of the method throws an exception.  For creating a Throws Advice our class must implement ThrowsAdvice interface.
  • ThrowsAdvice is a marker interface given in org.spfw.aop.*; package, and there are no methods in this interface to provide implementation :-)
  • while creating a ThrowsAdvice class in spring AOP, we should implement our services in a method called afterThrowing() with 1 or 4 parameter(s).
  • In fact, afterThrowing() method is not given in ThrowsAdvice, but we should implement our services in afterThrowing() method only because when ever an Exception is occurred in the business logic then the IOC container internally calls afterThrowing() method to apply the services.

Syntax For ThrowsAdvice Implementation

[ or ]

In case of single parameter, only Exception details are accessible, but in case of 4 parameter method, apart from Exception details we can also access method name, method parameters.  At the time of creating ThrowsAdvice, the method afterThrowing() can write any number of times, i mean we can write individual afterThrowing() methods for each Exception separately.

If we write multiple afterThrowing() methods in a class then the IOC container will give the preference like follows..

  • Verify whether afterThrowing() method is available with specific Exception type as parameter or not, if exist then the container calls that afterThrowing() method and executes services implemented in it.
  • If a specific Exception type of afterThrowing() method is not available, then container verifies whether an afterThrowing() method with 4 parameters exist or not if exist then executes the services in it.
  • If 4 parameters afterThrowing() is not exist in the class then the container verifies whether an afterThrowing() method with super class Exception parameter (Exception e) exists or not, if exists then executes the services implemented in it

Example On Spring AOP ThrowsAdvice

Files required…

  • MyImplClass.java
  • MyInterFace.java
  • MyThrowsAdvice.java
  • OurLogic.java
  • spconfig.xml

MyInterFace.java

MyImplClass.java

MyThrowsAdvice.java

OurLogic.java

spconfig.xml

Output:

 

Explanation:

  • Actually we know 10/0 will gives arithmetic exception, in MyThrowsAdvice.java i have written afterThrowing(ArithmeticException e) so IOC will executed this method only in the about output.
  • While you are executing this application, if you remove afterThrowing(ArithmeticException e) method then IOC will give the preference to afterThrowing(Exception e), and then to afterThrowing(Method m, Object args[], Object target,Exception e).





By [ Java/J2EE Developer & Web Designer ]
Founder of Java4s.com - Indian, lives in United States. You can contact me at sivateja [at] java4s.com
Follow me on


Comments

7 Responses to “Spring AOP Throws Advice Example With Complete Explanation”
  1. naidu says:

    ” Actually we know 10/0 will gives arithmetic exception, in MyThrowsAdvice.java i have written afterThrowing(ArithmeticException e) so IOC will executed this method only in the about output.
    While you are executing this application, remove this method then IOC will give the preference to the afterThrowing(Method m, Object args[], Object target,Exception e), if we remove this preference goes to afterThrowing(Exception e)”

    But I found in practical as The Preference order is

    1) afterThrowing(ArithmeticException e)
    2) afterThrowing(Exception e)
    3) afterThrowing(Method m, Object args[], Object target,Exception e)

  2. Java4s says:

    @Naidu

    You are correct ;)

    The correct order is…

    1) afterThrowing(ArithmeticException e)
    2) afterThrowing(Exception e)
    3) afterThrowing(Method m, Object args[], Object target,Exception e)

    Thanks for letting us aware of this.

    [Article Updated_On 13th Sept 2012]
  3. KiranV says:

    You have applied and updated the Naidu’s comment at your explanation for example.. But description of Throws Advice before example still shows wrong order.. Please update there also.

  4. Ramu says:

    But Practically I got this order

    1) afterThrowing(ArithmeticException e)
    2) afterThrowing(Method m, Object args[], Object target,Exception e)
    3) afterThrowing(Exception e)

    Thanks,
    Ramu

  5. Lakshman says:

    Hi Ramu,

    how u got this can u explain in simple way.

    we got

    1) afterThrowing(ArithmeticException e)
    2) afterThrowing(Exception e)
    3) afterThrowing(Method m, Object args[], Object target,Exception e)

    so,that we also correct our answer.

  6. In MyThrowsAdvice.java, line no 9,15,2
    shows the preference of the method invocation from
    IOC container but after execution this program section you have given one
    explaination section in which you have mentioned “if you remove afterThrowing
    (ArithmeticException e) method then IOC will give the preference to
    afterThrowing(Exception e), and then to afterThrowing(Method m, Object args[],
    Object target,Exception e).”

    So don’t you think its contradicting or which one is correct.Please correct me if i
    am misunderstanding.

  7. satya says:

    While implementing ThrowsAdvice my class showing inconsistent class Hierarchy! What i DO?

Name*
Mail*
Website



By posting your answer, you agree to our comments policy.
Most Recent Tutorials
Hibernate Recent Posts
Spring Recent Posts
Struts Recent Posts
Recomandded Links
Current & UpComing Tutorials
Sitemap :
spring Hibernate struts Json Ajax Log4j Log4j coreJava Servlets Web Services


Upcoming
  Web Services [ In Progress ], JSP
Facebook_Java4s  Twitter_Java4s  GooglePlus_Java4s  Youtube_Java4s 
© 2010 - 2014 Java4s - Get It Yourself.
The content is copyrighted to Sivateja Kandula and may not be reproduced on other websites.