简体   繁体   English

EnableAutoConfiguration spring注释如何工作?

[英]How does the EnableAutoConfiguration spring annotation work?

I am no fan of gross over abstractions, And i think Spring has committed a major felony. 我不喜欢粗略的抽象, 我认为Spring已经犯下了重大的重罪。

But I'm willing to overlook it this time if someone can explain the algorithm behind the 'auto' configuration. 但是如果有人能够解释“自动”配置背后的算法,我愿意忽略它。

Having a look at spring's own javadocs , It doesn't give much away other than saying that it will intelligently guess what you need and something to do about conditional beans. 看一下spring自己的javadocs ,除了说它会智能地猜测你需要什么以及对条件bean做些什么之外,它并没有给予太大的帮助。

Does someone know what algorithm is used to determine what needs to be loaded? 有人知道用什么算法来确定需要加载的内容吗?

In my experience as a Spring Boot user the basic factors for Spring Boot to decide on what auto-configurations will be enabled are: 根据我作为Spring Boot用户的经验,Spring Boot决定启用哪些自动配置的基本因素是:

1) The classes present on the classpath. 1)类路径上的类。 For example if RabbitMQ and Spring AMQP classes are present, then the RabbitAutoConfiguration will be enabled. 例如,如果存在RabbitMQ和Spring AMQP类,则将启用RabbitAutoConfiguration The corresponding annotation is @ConditionalOnClass , 相应的注释是@ConditionalOnClass

2) The presence or not of user defined beans. 2)是否存在用户定义的bean。 For example, if all the Spring Data JPA is present on the classpath, Spring Boot will register a LocalContainerEntityManagerFactoryBean bean only if the user has not already done so. 例如,如果类路径上存在所有Spring Data JPA,则只有在用户尚未注册时,Spring Boot才会注册LocalContainerEntityManagerFactoryBean bean。 The beans registered by the user will 'override' the default ones. 用户注册的bean将“覆盖”默认bean。 The relevant annotation is @ConditionalOnMissingBean 相关注释是@ConditionalOnMissingBean

As @DaveSyer mentions, you can of course use Spring Boot without @EnableAutoConfiguration if you want to include the relevant configuration on your own. 正如@DaveSyer所提到的,如果你想自己包含相关配置,你当然可以在没有@EnableAutoConfiguration情况下使用Spring Boot。 Or you could use the less drastic solution of the exclude field of @EnableAutoConfiguration . 或者你可以使用@EnableAutoConfigurationexclude字段的exclude那么激烈的解决方案。 If for example you want Spring Boot to autoconfigure everything except ActiveMQ, you would use @EnableAutoConfiguration(exclude=ActiveMQAutoConfiguration.class) 例如,如果您希望Spring Boot自动配置除ActiveMQ之外的所有内容,您将使用@EnableAutoConfiguration(exclude=ActiveMQAutoConfiguration.class)

In my opinion, there is absolutely no felony here! 在我看来,这里绝对没有重罪! You can use what you want from Spring Boot. 你可以使用你想要的Spring Boot。 When you don't want something it has to offer, you can easily opt out partially or completely! 如果您不想要它提供的东西,您可以轻松地部分或完全退出!

Also if you want to get a look under the covers, you can add the property 此外,如果您想了解封面,您可以添加该属性

logging.level.org.springframework.boot=DEBUG

to application.properties and Spring Boot will gladly give a detailed report of what was auto-configured and what wasn't 对于application.properties和Spring Boot,很乐意详细报告自动配置和未配置的内容

There is some documentation in the Spring Boot Reference Guide . Spring Boot Reference Guide中有一些文档。 It's not terribly complicated, and I hardly think it's a felony to just include a bunch of @Configuration that you might have written anyway (because that's all it does). 它并不是非常复杂,我几乎不认为只是包含一堆你可能已经写过的@Configuration是重罪的(因为它就是这样)。 Feel free not to use @EnableAutoConfiguration if you prefer to include the individual configurations individually. 如果您希望单独包含各个配置,请随意不要使用@EnableAutoConfiguration

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

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