简体   繁体   English

条件注解在 Spring Boot 中是如何工作的?

[英]How does Conditional annotation works in Spring Boot?

I understand that Spring Boot has lots of @Conditional annotations like, @ConditionalOnBean , @ConditionalOnClass , @ConditionalOnProperty , ConditionalOnWebApplication .我知道 Spring Boot 有很多@Conditional注释,例如@ConditionalOnBean@ConditionalOnClass@ConditionalOnPropertyConditionalOnWebApplication But I do not know how this work?但是我不知道这个是怎么工作的?

For Example:例如:

@Configuration    
@ConditionalOnClass(MyBean.class)
public class MyConfiguration{
    // omitted       
}

What I understood is, MyConfiguration will be loaded only if MyBean is available in my classpath.我的理解是,仅当MyBean在我的类路径中可用时,才会加载MyConfiguration But how would it compile and run if MyBean class is not in my class path as the compiler reaches to @ConditionalOnClass(MyBean.class) line, wont it throw compiler error?但是,当编译器到达@ConditionalOnClass(MyBean.class)行时,如果MyBean类不在我的类路径中,它将如何编译和运行,它不会抛出编译器错误吗? As soon as I add such code in my eclipse, I am getting compile time error.一旦我在我的 Eclipse 中添加这样的代码,我就会收到编译时错误。 Sorry if this is too basic question but I do not know what I am missing to understand.对不起,如果这是太基本的问题,但我不知道我错过了什么。

Spring Boot is being compiled with lots of optional dependencies; Spring Boot 正在编译时带有许多可选的依赖项; so when Spring Boot is compiled, the MyBean.class is on the classpath.所以在编译 Spring Boot 时, MyBean.class在类路径上。

Now your application may not have that MyBean.class in its classpath, but it doesn't fail at runtime.现在,您的应用程序的类路径中可能没有MyBean.class ,但它不会在运行时失败。 This is because the infrastructure that processes @ConditionalOnClass annotations will actually read the bytecode of the configuration and will only load them if that MyBean.class is present.这是因为处理@ConditionalOnClass注释的基础结构实际上会读取配置的字节码,并且只有在MyBean.class存在时才会加载它们。 See @ConditionalOnClass javadoc .请参阅@ConditionalOnClass javadoc

Now auto-configuration is a broad subject, and you can learn more about this in this talk .现在自动配置是一个广泛的主题,您可以在本次演讲中了解更多相关信息

As the Spring Boot Documentation says:正如Spring Boot 文档所说:

The @ConditionalOnClass and @ConditionalOnMissingClass annotations allows configuration to be included based on the presence or absence of specific classes. @ConditionalOnClass 和 @ConditionalOnMissingClass 注释允许根据特定类的存在或不存在来包含配置。 Due to the fact that annotation metadata is parsed using ASM you can actually use the value attribute to refer to the real class, even though that class might not actually appear on the running application classpath.由于注解元数据是使用 ASM 解析的,因此您实际上可以使用 value 属性来引用真实的类,即使该类实际上可能不会出现在正在运行的应用程序类路径中。 You can also use the name attribute if you prefer to specify the class name using a String value.如果您更喜欢使用 String 值指定类名,也可以使用 name 属性。

So they use the bytecode manipulation library ASM to be able to parse the class names during runtime, even if the classes are not anymore on the classpath.所以他们使用字节码操作库ASM来能够在运行时解析类名,即使类不再在类路径上。 Since Spring is open source, you can even just go look at the annotation reading code .由于 Spring 是开源的,你甚至可以直接去看注解阅读代码

You would typically use Optional dependencies:您通常会使用Optional依赖项:

<dependency>
    <groupId>org.freemarker</groupId>
    <artifactId>freemarker</artifactId>
    <optional>true</optional>
</dependency>

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

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