Announcement Announcement Module
No announcement yet.
Method afterThrow() in ThrowsAdvice did not get called Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Method afterThrow() in ThrowsAdvice did not get called


    I am having a problem in using ThrowAdvice class. I've been Googling + looking through this forum for a fully working example of ThrowAdvice.

    Basically, I have a basic class that throws an exception.

    package com.bla.cwt.webapp.service.impl;
    public class RetriveContactServiceImpl extends LDAPAuthentication implements RetriveContactService {
     public Contacts getContact(XmlObject vehicle, Credentials credential) throws  DataManagerException{

    I wanted to be able to capture the exception thrown by the method above. So I created the following Advice class:

    package com.bla.cwt.webapp.interceptors;
    import com.bla.cwt.webapp.service.DataManagerException;
    import com.bla.cwt.webapp.service.LDAPDataNotFoundException;
    import org.springframework.aop.ThrowsAdvice;
    import java.util.logging.Level;
    import java.util.logging.Logger;
    import java.lang.reflect.Method;
    public class ExceptionInterceptor implements ThrowsAdvice {
        private static final Logger logger = Logger.getLogger(ExceptionInterceptor.class.getName());
        public void afterThrowing(Method m, Object[] args, Object target, DataManagerException ldnfe) throws Throwable{
            logger.log(Level.WARNING, "----> ExceptionInterceptor ----> ", ldnfe);
    The spring configuration (applicationContext.xml) is as follow:

    <bean id="retrieveContactService" class="com.bla.cwt.webapp.service.impl.RetriveContactServiceImpl" singleton="false"/>
      <bean id="exceptionInterceptor" class="com.bla.cwt.webapp.interceptors.ExceptionInterceptor"/>
       <bean id="exceptionPointcutAdvisor" class="">
            <property name="advice" ref="exceptionInterceptor"/>
            <property name="pattern">
     <bean id="exceptionAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
                <property name="beanNames">
                <property name="interceptorNames">
    Inside my Struts Action class (yes, it's a webapp), I tried to get the service as follow:

     WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext( this.getServlet().getServletContext());
            RetriveContactService retriveContactService = (RetriveContactService) wac.getBean("retrieveContactService");
     try {
       contact = retriveContactService.getContact(vehicle, credential);
      }catch (DataManagerException e) {
          logger.log(Level.WARNING, "LDAP Data Manager Exception was caught ", e);

    The exception was thrown, but the code inside afterThrow() in ExceptionInterceptor has never been executed. Instead, the code inside the try{}catch{} gets executed. Can anyone tell me why?

    Also, basic question... what's the difference between <bean id="..." /> and <bean name="..."/> ??

    Thanks a lot for the help, and sorry for the basic question...



  • #2
    First off, your calling code will see a thrown exception even when a throws advice is used. If you don't throw a different exception from within the advice, your code will see the originally-thrown exception.

    To debug this, I would try manually creating the proxy--either in Java or by configuring a ProxyFactory instead of the autoproxy. That will help you determine if you should focus on the autoproxy or the advice itself as the problem.

    To your other question, "id" is defined as an XML ID attribute which has stricter syntax and special semantics (it must be unique in a document) when processed by a validating parser. It allows you to use the "local" attribute of a "ref" element to refer to it within the same configuration file. The "local" attribute is of type IDREF which is required to correspond to an ID attribute in the same document--thus the parser can perform rudimentary referential checks on your config before a bean factory sees it. The "name" attribute can be used instead of (or in addition to) the id attribute when the bean name would violate the ID syntax or when you want your bean to have more than one name (separate them with commas). I think bean names declared in the "name" attribute are called "aliases" in the documentation.



    • #3
      found better example of throws advice with spring 3

      Spring AOP Sample