Gday,
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.
Code: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:
The spring configuration (applicationContext.xml) is as follow:Code: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); } }
Inside my Struts Action class (yes, it's a webapp), I tried to get the service as follow:Code:<beans> <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="org.springframework.aop.support.RegexpMethodPointcutAdvisor"> <property name="advice" ref="exceptionInterceptor"/> <property name="pattern"> <value>.*</value> </property> </bean> <bean id="exceptionAutoProxy" class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"> <property name="beanNames"> <value>retrieveContactService</value> </property> <property name="interceptorNames"> <list> <value>exceptionPointcutAdvisor</value> </list> </property> </bean> </beans>
Code: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...
Regards,
Alex


Reply With Quote