官网上的解释是这样的:
Enable auto-configuration of the Spring Application Context, attempting to guess and configure beans that you are likely to need. Auto-configuration classes are usually applied based on your classpath and what beans you have defined.
Auto-configuration tries to be as intelligent as possible and will back-away as you define more of your own configuration. You can always manuallyexclude()
anyconfiguration that you never want to apply (use excludeName()
if you don'thave access to them). You can also exclude them via thespring.autoconfigure.exclude
property. Auto-configuration is always appliedafter user-defined beans have been registered.
The package of the class that is annotated with @EnableAutoConfiguration
has specific significance and is often used as a 'default'. For example, it will be usedwhen scanning for@Entity
classes. It is generally recommended that you place@EnableAutoConfiguration
in a root package so that all sub-packages and classescan be searched.
Auto-configuration classes are regular Spring Configuration
beans. They are located using the SpringFactoriesLoader
mechanism (keyed against this class).Generally auto-configuration beans are@Conditional
beans (mostoften using @ConditionalOnClass
and@ConditionalOnMissingBean
annotations).
首先,顾名思义,@EnableAutoConguration是和自动化配置相关的东西。
官网的第一段话主要是说,Spring应用上下文的自动化配置,尝试去猜测和配置你需要的bean。这些被自动化配置的类通常在classpath路径下,或者是你自己定义的bean。
第二段话主要是说,你可以使用exclude来排除那些你不想被自动化配置的类。
第三段是讲:被@AutoConfiguration注解的类所在的包有着特殊的意义,他们通常被认为是默认的包,并对其及下属的包进行扫描。
第四段前半句是说,被自动配置的类通常是在@Configuration里面配置的类。