2019/3/30
问题描述
在编写好异常通知和配置文件之后运行测试类,出现了错误:
in thread “main” java.lang.IllegalArgumentException: At least one handler method must be found in class [class aop.LogException]
解决思路
该报错已经将错误定位到了编写异常通知的类当中,发现在本类中找不到错误。于是进入本类的接口类ThrowsAdvice中进行核对发现源码中有一下描述:
* <pre class="code">void afterThrowing([Method, args, target], ThrowableSubclass);</pre>
* <p>Some examples of valid methods would be:
* <pre class="code">public void afterThrowing(Exception ex)</pre>
* <pre class="code">public void afterThrowing(RemoteException)</pre>
* <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, Exception ex)</pre>
* <pre class="code">public void afterThrowing(Method method, Object[] args, Object target, ServletException ex)</pre>
该段代码表示:
- 在接口实现类中必须按照格式编写
void afterThrowing([Method, args, target], ThrowableSubclass)
(中括号表示里面的参数要不全都存在,不要全都不存在!)
2.给了几个例子例如
public void afterThrowing(Exception ex)
可以发现,自己根据1中格式编写的方法必须时public。
解决方法
经过核对之后,发现自己写的接口实现类中的方法没有加 public 加以修饰,于是加上 public,解决了错误。