基於spring 3.1java
@Enable...系列能夠實現按需加載整個模塊,從而達到簡化配置的目的。咱們以@EnableCaching
爲例子spring
@Target({ElementType.TYPE}) @Retention(RetentionPolicy.RUNTIME) @Documented @Import({CachingConfigurationSelector.class}) public @interface EnableCaching { boolean proxyTargetClass() default false; AdviceMode mode() default AdviceMode.PROXY; int order() default 2147483647; }
因此魔法就在 @Import這裏,@Import能夠導入Configuration class, ImportSelector,ImportBeanDefinationRegister的實現類,至少申明一個@Bean方法的類,並將它們註冊爲bean。code
在上一篇中<context:component-scan/>
會將須要spring 註冊的bean定義爲BeanDefination。而後交由不一樣的bean. parser.component
spring3. 以後由ConfigurationClassPostProcessor
來處理@Configuration @Bean and @Component. 其中 ConfigurationClassParser.processConfigurationClass(ConfigurationClass)
處理了@Import 相關注解xml
// 加載註解對應的處理器 <context:annotation-config/> <context:component-scan base-package="com.xxx"/>
這兩種方式能夠作更多的自定義操做。get
因此在spring framework時代,@Component ,@Configuration 須要被裝載時,應用須要藉助@Import 或者@ComponentScan 的能力, 也是咱們一般須要經過@ComponentScan 掃描對應默認的包路徑。it
這樣存在的問題:io
因此催生出了spring boot的自動裝配。class