EnableAutoConfiguration spring注释如何工作? [英] How does the EnableAutoConfiguration spring annotation work?

查看:200
本文介绍了EnableAutoConfiguration spring注释如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不喜欢粗略的抽象,而且我认为Spring犯下了重大的重罪。

I am no fan of gross over abstractions, And i think Spring has committed a major felony.

但我愿意如果有人可以解释'自动'配置背后的算法,这次会忽略它。

But I'm willing to overlook it this time if someone can explain the algorithm behind the 'auto' configuration.

看看 spring自己的javadocs ,它除了说它会智能地猜测你需要什么以及对条件bean做些什么。

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.

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

Does someone know what algorithm is used to determine what needs to be loaded?

推荐答案

根据我作为Spring Boot用户的经验,Spring Boot决定启用哪些自动配置的基本因素是:

In my experience as a Spring Boot user the basic factors for Spring Boot to decide on what auto-configurations will be enabled are:

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

1) The classes present on the classpath. For example if RabbitMQ and Spring AMQP classes are present, then the RabbitAutoConfiguration will be enabled. The corresponding annotation is @ConditionalOnClass,

2)是否存在用户定义的bean。例如,如果类路径上存在所有Spring Data JPA,则仅当用户尚未注册时,Spring Boot才会注册 LocalContainerEntityManagerFactoryBean bean。用户注册的bean将覆盖默认bean。相关的注释是 @ConditionalOnMissingBean

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 LocalContainerEntityManagerFactoryBean bean 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

正如@DaveSyer所提到的,你当然可以使用没有<$ c的Spring Boot $ c> @EnableAutoConfiguration 如果您想自己包含相关配置。或者您可以使用排除字段 @EnableAutoConfiguration 的不那么激烈的解决方案。例如,如果您希望Spring Boot自动配置除ActiveMQ之外的所有内容,您将使用 @EnableAutoConfiguration(exclude = ActiveMQAutoConfiguration.class)

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

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

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!

此外,如果您想了解封面,您可以添加属性

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

logging.level.org.springframework.boot = DEBUG

application.properties ,Spring Boot很乐意详细报告自动配置的内容以及不是什么

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

这篇关于EnableAutoConfiguration spring注释如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆