Java EnableAutoConfiguration 弹簧注解是如何工作的?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24351581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 11:43:26  来源:igfitidea点击:

How does the EnableAutoConfiguration spring annotation work?

javaspringspring-boot

提问by coderatchet

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?

有人知道用什么算法来确定需要加载什么吗?

采纳答案by geoand

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. For example if RabbitMQ and Spring AMQP classes are present, then the RabbitAutoConfigurationwill be enabled. The corresponding annotation is @ConditionalOnClass,

1) 存在于类路径上的类。例如,如果存在 RabbitMQ 和 Spring AMQP 类,则将RabbitAutoConfiguration启用它们。对应的注解是@ConditionalOnClass

2) The presence or not of user defined beans. For example, if all the Spring Data JPA is present on the classpath, Spring Boot will register a LocalContainerEntityManagerFactoryBeanbean only if the user has not already done so. The beans registered by the user will 'override' the default ones. The relevant annotation is @ConditionalOnMissingBean

2) 用户定义的bean 是否存在。例如,如果所有 Spring Data JPA 都存在于 classpath 中,则 Spring BootLocalContainerEntityManagerFactoryBean只会在用户尚未注册bean 的情况下注册。用户注册的 bean 将“覆盖”默认的 bean。相关的注释是@ConditionalOnMissingBean

As @DaveSyer mentions, you can of course use Spring Boot without @EnableAutoConfigurationif you want to include the relevant configuration on your own. Or you could use the less drastic solution of the excludefield of @EnableAutoConfiguration. If for example you want Spring Boot to autoconfigure everything except ActiveMQ, you would use @EnableAutoConfiguration(exclude=ActiveMQAutoConfiguration.class)

正如@DaveSyer 提到的,@EnableAutoConfiguration如果您想自己包含相关配置,当然可以使用 Spring Boot 。或者你可以使用的较温和的解决方案exclude的领域@EnableAutoConfiguration。例如,如果您希望 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. When you don't want something it has to offer, you can easily opt out partially or completely!

在我看来,这里绝对没有重罪!你可以使用你想要的 Spring Boot。当您不想要它提供的某些东西时,您可以轻松选择部分或完全退出!

Also if you want to get a look under the covers, you can add the property

另外,如果你想看看封面,你可以添加属性

logging.level.org.springframework.boot=DEBUG

logging.level.org.springframework.boot=DEBUG

to application.propertiesand Spring Boot will gladly give a detailed report of what was auto-configured and what wasn't

toapplication.properties和 Spring Boot 会很高兴地详细报告哪些是自动配置的,哪些不是

回答by Dave Syer

There is some documentation in the Spring Boot Reference Guide. It's not terribly complicated, and I hardly think it's a felony to just include a bunch of @Configurationthat you might have written anyway (because that's all it does). Feel free not to use @EnableAutoConfigurationif you prefer to include the individual configurations individually.

Spring Boot 参考指南中有一些文档。它并不是非常复杂,而且我几乎不认为只包含一堆@Configuration你可能已经写过的东西是重罪(因为它就是这样)。@EnableAutoConfiguration如果您更喜欢单独包含各个配置,请随意不要使用。