1. 程式人生 > >Spring Java Configuration之@EnableAutoConfiguration

Spring Java Configuration之@EnableAutoConfiguration

官網上的解釋是這樣的:

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 manually

exclude() 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裡面配置的類。