Section 6.4. Advising Exceptions

team bbl


6.4. Advising Exceptions

Often, you'll want to attach a service to exception logic, rather than mainstream code. It's especially important when exceptions force a major change in application flow, such as rolling back when an exception occurs, or automatically notifying administrators when some resource runs low.

Your application schedules bikes for a small store. You can use Spring's exceptions to generate a message whenever something goes wrong in the application. For simplicity, you'll send it to the console for now.

6.4.1. How do I do that?

The first job is to build an advisor. Use a simple class that implements the THRowsAdvice interface, like in Example 6-10.

Example 6-10. ExceptionInterceptor.java
public class ExceptionInterceptor implements ThrowsAdvice {     public void afterThrowing(Method m, Object[] args,             Object target, Exception ex) {         System.out.println("Call to method " + m.getName( ) +            " on class " + target.getClass( ).getName( ) +             " resulted in exception of type " + ex.getClass( ).getName( ));         System.out.println("Exception message: " + ex.getMessage( ));     } }

Keep in mind that the THRowsAdvice interface contains no method signatures; you can implement as many versions of afterThrowing as you want. Each must declare that last parameter as a subclass of Throwable. The other three parameters are optional.

Configure the parameters for the advice interceptor in the context. Next, you'll configure the advice (note that Example 6-11 goes back to using the ProxyFactoryBean from the first part of this chapter, as opposed to an autoproxy, like the previous example).

Example 6-11. RentABike-servlet.xml
<bean      /> <bean      >       <property name="proxyInterfaces">       <value>com.springbook.RentABike</value>    </property>    <property name="interceptorNames">       <list>          <value>exceptionInterceptor</value>          <value>transactionInterceptor</value>          <value>saveAdvisor</value>          <value>rentaBikeTarget</value>       </list>    </property> </bean>

In this case, notice that we're still targeting methods. You'll get notified whenever a serious exception occurs. To make sure it's working, let's try to remove a bike that doesn't exist (Example 6-12).

Example 6-12. ControllerTest.java
public void testRemoveNonExistentBike( ) throws Exception {    Bike b = new Bike(99, "me", "mine", 1, "1", 12.00, "good");    store.deleteBike(b); }

The console output in Example 6-13 is the result.

Example 6-13. Standard output from running application
Call to method deleteBike on class $Proxy0 resulted in exception of type  org.springframework.orm.hibernate.HibernateSystemException Exception message: Batch update row count wrong: 0; nested exception is  net.sf.hibernate.HibernateException: Batch update row count wrong: 0

6.4.2. What just happened?

There's no magic here. Spring uses the same mechanism, proxies, to intercept exception logic. We told the proxy to attach our interceptor to all of the methods that updated the database. Within our exception logic, we can decide what to do. In our case, we looked at the exception and decided which exceptions were important to us. We then simply dumped some information to the console, and returned control where it belonged: in this case, the façade method.

    team bbl



    Spring. A developer's Notebook
    Spring: A Developers Notebook
    ISBN: 0596009100
    EAN: 2147483647
    Year: 2005
    Pages: 90

    flylib.com © 2008-2017.
    If you may any questions please contact us: flylib@qtcs.net