简体   繁体   English

带有@Conditional 的完全限定类名

[英]Fully qualified class name with @Conditional

In a course video about spring boot and auto-configuration在有关 Spring Boot 和自动配置的课程视频中

在此处输入图片说明

It say that we could use a fully qualified class name type = { "abcFoo" } instead of value = { Foo.class } if we don't want to depend on the class object at compile time.它说如果我们不想在编译时依赖类对象,我们可以使用完全限定的类名type = { "abcFoo" }而不是value = { Foo.class }

Can you explain me what that means ?你能解释一下这是什么意思吗?

Using type = { "abcFoo" } will compile even if there is no class abcFoo on your classpath, while value = { abcFoo.class } requires Foo to be on your classpath at least at compile time.即使类abcFoo上没有类abcFoo ,使用type = { "abcFoo" }也会编译,而value = { abcFoo.class }要求Foo至少在编译时位于类路径上。 Due to the nature of the annotations both variants can be executed even if abcFoo is not present on the classpath on runtime - at least under some circumstances:由于注释的性质,即使abcFoo在运行时的类路径中不存在,也可以执行这两个变体 - 至少在某些情况下:

JavaDoc of @ConditionalOnClass.value @ConditionalOnClass.value JavaDoc

Since this annotation is parsed by loading class bytecode, it is safe to specify classes here that may ultimately not be on the classpath, only if this annotation is directly on the affected component and not if this annotation is used as a composed, meta-annotation.由于此注解是通过加载类字节码来解析的,因此在此处指定最终可能不在类路径上的类是安全的,前提是此注解直接位于受影响的组件上,而不是将此注解用作组合的元注解. In order to use this annotation as a meta-annotation, only use the {@link #name} attribute.为了将此注释用作元注释,请仅使用 {@link #name} 属性。

In general the @ConditionalOnClass annotation can be used to execute configurations that depend on other classes to be present.通常, @ConditionalOnClass注释可用于执行依赖于其他类的配置。

For example you could have different Cloud storage configurations AWS, GCP and Azure.例如,您可以拥有不同的云存储配置 AWS、GCP 和 Azure。 Each of these configurations registers a CloudUpload bean.这些配置中的每一个都注册了一个CloudUpload bean。 And with @ConditionalOnClass you execute only the configuration that is currently valid, ie if the AWS SDK is on the classpath, you configure the S3CloudUpload bean, ...并且使用@ConditionalOnClass您只执行当前有效的配置,即如果 AWS SDK 在类路径上,您配置S3CloudUpload bean,...

That is simply by adapting your deployment/classpath you configure your software in a different way.这只是通过调整您的部署/类路径,您可以以不同的方式配置您的软件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM