private void initControllerAdviceCaches(ApplicationContext applicationContext) { List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(applicationContext); AnnotationAwareOrderComparator.sort(beans); Class<?> beanType = bean.getBeanType(); if (beanType != null) { Set<Method> attrMethods = MethodIntrospector.selectMethods(beanType, MODEL_ATTRIBUTE_METHODS);
@SuppressWarnings("unchecked") private <A> List<A> getMatchingAdvice(MethodParameter parameter, Class<? extends A> adviceType) { List<Object> availableAdvice = getAdvice(adviceType); if (CollectionUtils.isEmpty(availableAdvice)) { return Collections.emptyList(); } List<A> result = new ArrayList<>(availableAdvice.size()); for (Object advice : availableAdvice) { if (advice instanceof ControllerAdviceBean) { ControllerAdviceBean adviceBean = (ControllerAdviceBean) advice; if (!adviceBean.isApplicableToBeanType(parameter.getContainingClass())) { continue; } advice = adviceBean.resolveBean(); } if (adviceType.isAssignableFrom(advice.getClass())) { result.add((A) advice); } } return result; }
/** * Find the names of beans annotated with * {@linkplain ControllerAdvice @ControllerAdvice} in the given * ApplicationContext and wrap them as {@code ControllerAdviceBean} instances. */ public static List<ControllerAdviceBean> findAnnotatedBeans(ApplicationContext context) { return Arrays.stream(BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context, Object.class)) .filter(name -> context.findAnnotationOnBean(name, ControllerAdvice.class) != null) .map(name -> new ControllerAdviceBean(name, context)) .collect(Collectors.toList()); }
@Override @Nullable public Class<?> getBeanType() { return this.adviceBean.getBeanType(); }
@Override public boolean isApplicableToBeanType(Class<?> beanType) { return this.adviceBean.isApplicableToBeanType(beanType); }
private void initControllerAdviceCache() { ApplicationContext context = getApplicationContext(); if (context == null) { return; } if (logger.isTraceEnabled()) { logger.trace("Looking for @MessageExceptionHandler mappings: " + context); } List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(context); AnnotationAwareOrderComparator.sort(beans); initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(beans)); }
@Override public Object resolveBean() { return this.adviceBean.resolveBean(); }
this.order = initOrderFromBeanType(beanType); this.order = initOrderFromBean(bean);
/** * Return a bean instance if necessary resolving the bean name through the BeanFactory. */ public Object resolveBean() { return (this.bean instanceof String ? obtainBeanFactory().getBean((String) this.bean) : this.bean); }
@Override public int getOrder() { return this.adviceBean.getOrder(); } }
private static int initOrderFromBean(Object bean) { return (bean instanceof Ordered ? ((Ordered) bean).getOrder() : initOrderFromBeanType(bean.getClass())); }
Assert.notNull(bean, "Bean must not be null"); beanType = bean.getClass(); this.order = initOrderFromBean(bean);
@SuppressWarnings("unchecked") static <T> List<T> getAdviceByType(@Nullable List<Object> requestResponseBodyAdvice, Class<T> adviceType) { if (requestResponseBodyAdvice != null) { List<T> result = new ArrayList<>(); for (Object advice : requestResponseBodyAdvice) { Class<?> beanType = (advice instanceof ControllerAdviceBean ? ((ControllerAdviceBean) advice).getBeanType() : advice.getClass()); if (beanType != null && adviceType.isAssignableFrom(beanType)) { result.add((T) advice); } } return result; } return Collections.emptyList(); }
private void assertApplicable(String message, ControllerAdviceBean controllerAdvice, Class<?> controllerBeanType) { assertNotNull(controllerAdvice); assertTrue(message, controllerAdvice.isApplicableToBeanType(controllerBeanType)); }
private void initControllerAdviceCache() { ApplicationContext context = getApplicationContext(); if (context == null) { return; } if (logger.isTraceEnabled()) { logger.trace("Looking for @MessageExceptionHandler mappings: " + context); } List<ControllerAdviceBean> beans = ControllerAdviceBean.findAnnotatedBeans(context); AnnotationAwareOrderComparator.sort(beans); initMessagingAdviceCache(MessagingControllerAdviceBean.createFromList(beans)); }
@Override public Object resolveBean() { return this.adviceBean.resolveBean(); }
this.order = initOrderFromBeanType(beanType); this.order = initOrderFromBean(bean);
/** * Return a bean instance if necessary resolving the bean name through the BeanFactory. */ public Object resolveBean() { return (this.bean instanceof String ? obtainBeanFactory().getBean((String) this.bean) : this.bean); }
@Override public int getOrder() { return this.adviceBean.getOrder(); } }
private static int initOrderFromBean(Object bean) { return (bean instanceof Ordered ? ((Ordered) bean).getOrder() : initOrderFromBeanType(bean.getClass())); }