Thread.setDefaultUncaughtExceptionHandler in Spring

I am developing a Spring application and wondering if there is any part of the framework that allows me to do something like this in a more elegant way, like setting up something in XML?

+5
source share
2 answers

If the goal of your question is to set a custom UncaughtExceptionHandlerone through the context of your application, you can use:

<bean id="myhandler" class="java.lang.ThreadGroup">
    <constructor-arg value="Test"/>
</bean>

<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
   <property name="targetClass" value="java.lang.Thread"/>
   <property name="targetMethod" value="setDefaultUncaughtExceptionHandler"/>
   <property name="arguments">
     <list>
         <ref bean="myhandler" />
     </list>
   </property>
</bean>

(NB replace myhandler with the Thread.UncaughtExceptionHandlerchoice ...)

+5
source

@ControllerAdvice . https://spring.io/blog/2013/11/01/exception-handling-in-spring-mvc, :

@ControllerAdvice
public class MyUncaughtExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public void defaultExceptionHandler(Exception e) {
        // do whatever you want with the exception
    } 
}
+1

All Articles