博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
说说 Spring Boot 的条件化注解
阅读量:1904 次
发布时间:2019-04-26

本文共 1474 字,大约阅读时间需要 4 分钟。

SpringBoot 定义了许多条件化注解,可以将它们用到配置类上,以说明生效条件。

条件化注解 生效条件
@ConditionalOnBean 配置了特定的 Bean。
@ConditionalOnMissingBean 没有配置特定的 Bean。
@ConditionalOnClass Classpath 里有指定的类。
@ConditionalOnMissingClass Classpath 里没有指定的类。
@ConditionalOnExpression 给定的 SpringExpressionLanguage(SpEL) 表达式计算结果为 true。
@ConditionalOnJava Java 的版本匹配特定值或者一个范围值。
@ConditionalOnJndi JNDI 参数必须存在。
@ConditionalOnProperty 指定的属性有一个明确的值。
@ConditionalOnResource Classpath 里存在指定的资源。
@ConditionalOnWebApplication 是一个 Web 应用程序。
@ConditionalOnNotWebApplication 不是一个 Web 应用程序。

比如 SpringBootWebSecurityConfiguration.java 的源码为:

@Configuration@ConditionalOnClass(WebSecurityConfigurerAdapter.class)@ConditionalOnMissingBean(WebSecurityConfigurerAdapter.class)@ConditionalOnWebApplication(type = Type.SERVLET)public class SpringBootWebSecurityConfiguration {	@Configuration	@Order(SecurityProperties.BASIC_AUTH_ORDER)	static class DefaultConfigurerAdapter extends WebSecurityConfigurerAdapter {	}}

SpringBootWebSecurityConfiguration 配置了三个条件化注解,也就是说 SpringBootWebSecurityConfiguration 必须在这些条件都满足的情况下,才能生效:

  1. Classpath 里有 WebSecurityConfigurerAdapter.class。
  2. 没有配置 WebSecurityConfigurerAdapter Bean。
  3. 是一个 Web 应用程序。

其中 ConditionalOnWebApplication 注解存在这些类型:

参数 说明
SERVLET 基于 servlet 的 web 应用程序。
REACTIVE 基于 reactive 的 web 应用程序。
ANY 上述两种都可以。

基于 reactive 的 web 应用程序是 Spring 5 新增的模块,名为 spring-webflux。 它可以用少量线程来处理 request 和 response io 操作,这些线程统称为 Loop 线程。那些业务中阻塞操作,会提交给响应式框架的 work 线程进行处理,而那些不阻塞的操作则在 Loop 线程中进行处理。通过这种方式,可以大大提高 Loop 线程的利用率。

转载地址:http://jrbcf.baihongyu.com/

你可能感兴趣的文章
pip install git+
查看>>
UGC 用户产生内容
查看>>
ranger
查看>>
slurm
查看>>
xfce4
查看>>
xrdp
查看>>
Raft算法
查看>>
Python计算文本BLEU分数
查看>>
swap内存(linux)
查看>>
人脸au
查看>>
torch.distributed 分布式
查看>>
OpenMP编程模型(OMP)
查看>>
混合精度训练(FP16 & FP32)
查看>>
PyPy
查看>>
打印CSDN文章
查看>>
MATLAB与CUDA
查看>>
Linux png转jpg (convert命令)
查看>>
NAS (Network Attached Storage 网络附属存储)
查看>>
Ubuntu更新后终端中字体的颜色全是白色
查看>>
Ninja
查看>>