org.springframework.web.method.ControllerAdviceBean#isApplicableToBeanType(@Nullable Class<?> beanType)

判断当前类是否被ControllerAdviceBean修饰,判断依据位 ControllerAdviceBean上设置的注解@ControllerAdvice内的参数

package org.springframework.web.bind.annotation;

import java.lang.annotation.Annotation;
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import org.springframework.core.annotation.AliasFor;
import org.springframework.stereotype.Component; @Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface ControllerAdvice { @AliasFor("basePackages")
String[] value() default {}; @AliasFor("value")
String[] basePackages() default {}; Class<?>[] basePackageClasses() default {}; Class<?>[] assignableTypes() default {}; Class<? extends Annotation>[] annotations() default {};
}

其内部使用 HandlerTypePredicate beanTypePredicate 进行判断,如果@ControllerAdvice注解内无参数 则可以修饰所有类。

由该段代码判断:

ControllerAdvice annotation = (beanType != null ?
AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class) : null); if (annotation != null) {
this.beanTypePredicate = HandlerTypePredicate.builder()
.basePackage(annotation.basePackages())
.basePackageClass(annotation.basePackageClasses())
.assignableType(annotation.assignableTypes())
.annotation(annotation.annotations())
.build();
}
else {
this.beanTypePredicate = HandlerTypePredicate.forAnyHandlerType();
}
HandlerTypePredicate.forAnyHandlerType(); 改方法实现如下:
return new HandlerTypePredicate(Collections.emptySet(), Collections.emptyList(), Collections.emptyList());

=====

实际会调用org.springframework.web.method.HandlerTypePredicate#test

if (!hasSelectors()) {
return true;
}
org.springframework.web.method.HandlerTypePredicate#hasSelectors

private boolean hasSelectors() {
return (!this.basePackages.isEmpty() || !this.assignableTypes.isEmpty() || !this.annotations.isEmpty());
}

org.springframework.web.method.ControllerAdviceBean#isApplicableToBeanType 作用的更多相关文章

  1. org.springframework.web.method.HandlerMethod 与 org.springframework.messaging.handler.HandlerMethod 转换失败

    Springmvc hander.getclassclass org.springframework.web.method.HandlerMethod HandlerMethod.classclass ...

  2. org.springframework.web.filter.DelegatingFilterProxy的作用

    一.类结构 DelegatingFilterProxy类继承GenericFilterBean,间接实现了Filter,故而该类属于一个过滤器.那么就会有实现Filter中init.doFilter. ...

  3. 使用ControllerAdvice注意事项,Ambiguous @ExceptionHandler method mapped for [class org.springframework.web.bind.MethodArgumentNotValidException]

    前言 ControllerAdvice非常好用,可以把系统内部的异常统一处理.用起来也很简单.比如,http://www.cnblogs.com/woshimrf/p/spring-web-400.h ...

  4. org.springframework.web.bind.ServletRequestDataBinde

    org.springframework.validation Class DataBinder java.lang.Object org.springframework.validation.Data ...

  5. SpringMVC文件上传报错org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

    错误信息: java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to or ...

  6. org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

    异常信息 org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable represen ...

  7. Java之——java.lang.NoSuchMethodException: [org.springframework.web.multipart.MultipartFile;.()

    转自:https://blog.csdn.net/l1028386804/article/details/65449355 ava.lang.NoSuchMethodException: [org.s ...

  8. java.lang.ClassCastException: org.apache.catalina.connector.RequestFacade cannot be cast to org.springframework.web.multipart.MultipartHttpServletRequest

    转自:https://blog.csdn.net/iteye_17476/article/details/82651580 java.lang.ClassCastException: org.apac ...

  9. org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not supported

    1:先上控制台报错信息 org.springframework.web.HttpRequestMethodNotSupportedException: Request method 'PUT' not ...

随机推荐

  1. linux下apache和tomcat整合

    一 Apache与Tomcat比较联系 apache支持静态页,tomcat支持动态的,比如servlet等. 一般使用apache+tomcat的话,apache只是作为一个转发,对jsp的处理是由 ...

  2. RHEL6+GFS2+MYSQL高可用

    RHCS集群安装部署 组件介绍: luci: luci是一个基于web的,用来管理和配置RHCS集群,通过luci可以轻松的搭建一个功能强大的集群系统,节点主机可以使用ricci来和luci 管理段进 ...

  3. Python 去除文件中的空行

    def clear_space(): with open("test","r",encoding="utf-8") as fr: for l ...

  4. Python paramiko安装报错

    报错:CryptographyDeprecationWarning 代码引用: import paramiko client = paramiko.SSHClient() client.connect ...

  5. zabbix--邮件告警报错“Support for SMTP authentication was not compiled in”

    zabbix 邮件告警报错“Support for SMTP authentication was not compiled in” 邮件报警失败:Support for SMTP authentic ...

  6. python小案例-计算输入两个数的最大公约数与最小公倍数

    # 计算最大公约数 def gcd(x,y): """ 计算最大公约数 :param x:一个正整数 :param y:一个正整数 :return:x,y的最大公约数 & ...

  7. Objective-C 消息发送与转发机制原理(摘)

    八面玲珑的 objc_msgSend 此函数是消息发送必经之路,但只要一提 objc_msgSend,都会说它的伪代码如下或类似的逻辑,反正就是获取 IMP 并调用: id objc_msgSend( ...

  8. js版本规范的表示:ES6 == ES 6 == ECMAScript 6 == ECMA-262 6

    Ecma 国际大会宣布正式批准ECMA-262第 6 版,亦即 ECMAScript 2015(曾用名:ECMAScript 6.ES6)的语言规范. 关系 ECMA-262 == ECMAScrip ...

  9. accept返回的socket的端口号和连接socket一样的!!! socket绑定信息结构

    今天与同学争执一个话题:由于socket的accept函数在有客户端连接的时候产生了新的socket用于服务该客户端,那么,这个新的socket到底有没有占用一个新的端口? 讨论完后,才发现,自己虽然 ...

  10. LVS+DR+apache+keepalived负载均衡

    1.首先准备两台服务器.三台也可以我这里是两台 IP:192.168.52.33 IP:192.168.52.34 VIP:192.168.52.100 2.关闭防火墙 systemctl  stop ...