In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces how to use SpringFramework's ControllerAdvice annotations, which has a certain reference value, interested friends can refer to, I hope you can learn a lot after reading this article, the following let the editor take you to understand it.
SpringFramework version of 5.0.9.release.
We handle exceptions through @ ControllerAdvice and @ ExceptionHandler. How does Springmvc handle them?
ControllerAdviceBean has an important method findAnnotatedBeans, such as List-1
List-1
Public class ControllerAdviceBean implements Ordered {... Public static List findAnnotatedBeans (ApplicationContext applicationContext) {List beans = new ArrayList (); String [] var2 = BeanFactoryUtils.beanNamesForTypeIncludingAncestors (applicationContext, Object.class); int var3 = var2.length; for (int var4 = 0; var4)
< var3; ++var4) { String name = var2[var4]; if (applicationContext.findAnnotationOnBean(name, ControllerAdvice.class) != null) { beans.add(new ControllerAdviceBean(name, applicationContext)); } } return beans; } ... 如List-1所示,从applicationContext中获取所有的ControllerAdvice注解的Bean,之后封装到ControllerAdviceBean中。 来看下ExceptionHandlerExceptionResolver,它的类继承图如下图1所示: 图1 ExceptionHandlerExceptionResolver是HandlerExceptionResolver,所以在Springmvc的doDispatch中会调用它。实现了InitializingBean,所以有afterPropertiesSet方法,如下List-2所示: List-2 @Overridepublic void afterPropertiesSet() { // Do this first, it may add ResponseBodyAdvice beans initExceptionHandlerAdviceCache(); ...}private void initExceptionHandlerAdviceCache() { ... List adviceBeans = ControllerAdviceBean.findAnnotatedBeans(getApplicationContext()); AnnotationAwareOrderComparator.sort(adviceBeans); for (ControllerAdviceBean adviceBean : adviceBeans) { Class beanType = adviceBean.getBeanType(); if (beanType == null) { throw new IllegalStateException("Unresolvable type for ControllerAdviceBean: " + adviceBean); } ExceptionHandlerMethodResolver resolver = new ExceptionHandlerMethodResolver(beanType); if (resolver.hasExceptionMappings()) { this.exceptionHandlerAdviceCache.put(adviceBean, resolver); if (logger.isInfoEnabled()) { logger.info("Detected @ExceptionHandler methods in " + adviceBean); } } if (ResponseBodyAdvice.class.isAssignableFrom(beanType)) { this.responseBodyAdvice.add(adviceBean); if (logger.isInfoEnabled()) { logger.info("Detected ResponseBodyAdvice implementation in " + adviceBean); } } }} List-2中,initExceptionHandlerAdviceCache方法调用List-1中ControllerAdviceBean的findAnnotatedBeans方法,获取所有ControllerAdvice的bean,之后排序,所以当有多个ControllerAdivce注解的类且需要排序时,可以实现spring的Order接口来实现。 之后遍历ControllerAdviceBean,之后获取Bean的class类,传入到ExceptionHandlerMethodResolver的构造方法中,如下List-3所示: List-3 public class ExceptionHandlerMethodResolver { ... public static final MethodFilter EXCEPTION_HANDLER_METHODS = method ->(AnnotationUtils.findAnnotation (method, ExceptionHandler.class)! = null); Private final Map
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.