Search code examples
javaspringuncaughtexceptionhandler

Thread.setDefaultUncaughtExceptionHandler in Spring


I am developing a Spring application, and was wondering if there's a part of the framework that lets me do something like this in a more elegant way, such as configuring something in XML?


Solution

  • If the purpose of your question is to set a custom UncaughtExceptionHandler through your application context, 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>
    

    (N.B. replace myhandler with an Thread.UncaughtExceptionHandler of choice...)