Fully qualified class name with @Conditional

Youssef :

In a course video about spring boot and auto-configuration

enter image description here

It say that we could use a fully qualified class name type = { "a.b.c.Foo" } instead of value = { Foo.class } if we don't want to depend on the class object at compile time.

Can you explain me what that means ?

dpr :

Using type = { "a.b.c.Foo" } will compile even if there is no class a.b.c.Foo on your classpath, while value = { a.b.c.Foo.class } requires Foo to be on your classpath at least at compile time. Due to the nature of the annotations both variants can be executed even if a.b.c.Foo is not present on the classpath on runtime - at least under some circumstances:

JavaDoc of @ConditionalOnClass.value

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.

In general the @ConditionalOnClass annotation can be used to execute configurations that depend on other classes to be present.

For example you could have different Cloud storage configurations AWS, GCP and Azure. Each of these configurations registers a CloudUpload bean. And with @ConditionalOnClass you execute only the configuration that is currently valid, i.e. if the AWS SDK is on the classpath, you configure the S3CloudUpload bean, ...

That is simply by adapting your deployment/classpath you configure your software in a different way.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=400544&siteId=1