如果启用了@Async,则默认的异常处理机制不起作用。要处理使用@Async注释的方法引发的异常,您需要实现一个自定义AsyncExceptionHandler。
public class AsyncExceptionHandler implements AsyncUncaughtExceptionHandler{ @Override public void handleUncaughtException(Throwable ex, Method method, Object... params) { // Here goes your exception handling logic. }}现在,您需要在Application类中将此customExceptionHandler配置为
@EnableAsyncpublic class Application implements AsyncConfigurer { @Override Executor getAsyncExecutor(){ // your ThreadPoolTaskExecutor configuration goes here. }@Overridepublic AsyncUncaughExceptionHandler getAsyncUncaughtExceptionHandler(){ return new AsyncExceptionHandler();}注意:确保为了使AsyncExceptionHandler工作,您需要在Application类中实现AsyncConfigurer。



