Spring Boot中@EnableAutoConfiguration和@SpringBootApplication注解的功能及联系说明
下文笔者讲述SpringBoot中@EnableAutoConfiguration和@SpringBootApplication注解的功能及联系说明,如下所示
@EnableAutoConfiguration注解的功能
@EnableAutoConfiguration注解的功能: 开启自动配置 帮助Spring Boot应用 将所有符合条件@Configuration注解 加载到当前Spring Boot的IoC容器中
@EnableAutoConfiguration注解的源码
@Target(ElementType.TYPE) @Retention(RetentionPolicy.Runtime) @Documented @Inherited @AutoConfigurationPackage @Import(AutoConfigurationImportSelector.class) public @interface EnableAutoConfiguration { String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration"; /** * Exclude specific auto-configuration classes such that they will never be applied. * @return the classes to exclude */ Class<?>[] exclude() default {}; /** * Exclude specific auto-configuration class names such that they will never be * applied. * @return the class names to exclude * @since 1.3.0 */ String[] excludeName() default {}; }
注解值说明: exclude: 排除特定的自动配置类 使其永远不会被应用 excludeName 排除特定的自动配置类名 使其永远不会被应用
@EnableAutoConfiguration注解的功能
Spring Boot 启动加载时排除数据库加载操作 如下: @EnableAutoConfiguration(exclude={ DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) @RestController @SpringBootApplication public class HelloWorldApplication { //... }
@EnableAutoConfiguration与@SpringBootApplication的关系
@SpringBootApplication 注解包含 @EnableAutoConfiguration 注解
@SpringBootApplication源码
@Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Inherited @SpringBootConfiguration @EnableAutoConfiguration @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) }) public @interface SpringBootApplication { //... }
版权声明
本文仅代表作者观点,不代表本站立场。
本文系作者授权发表,未经许可,不得转载。