前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >@SpringBootApplication 的作用是什么

@SpringBootApplication 的作用是什么

作者头像
水货程序员
修改2018-12-03 15:07:22
6.5K0
修改2018-12-03 15:07:22
举报
文章被收录于专栏:javathings

@SpringBootApplication 的作用是什么

@SpringBootApplication 标注的类为 Spring Boot 的主配置类,Spring Boot 会运行这个类的 main 方法来启动 Spring Boot 应用。

@SpringBootApplication 注解的定义如下:

代码语言:javascript
复制
@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 {
 
/**
* Exclude specific auto-configuration classes such that they will never be applied.
* @return the classes to exclude
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
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
*/
@AliasFor(annotation = EnableAutoConfiguration.class)
String[] excludeName() default {};
 
/**
* Base packages to scan for annotated components. Use {@link #scanBasePackageClasses}
* for a type-safe alternative to String-based package names.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] scanBasePackages() default {};
 
/**
* Type-safe alternative to {@link #scanBasePackages} for specifying the packages to
* scan for annotated components. The package of each class specified will be scanned.
* <p>
* Consider creating a special no-op marker class or interface in each package that
* serves no purpose other than being referenced by this attribute.
* @return base packages to scan
* @since 1.3.0
*/
@AliasFor(annotation = ComponentScan.class, attribute = "basePackageClasses")
Class<?>[] scanBasePackageClasses() default {};
 
}

说明

@SpringBootApplication 注解等价于以同时使 用 @SpringBootConfiguration,@EnableAutoConfiguration 和@ComponentScan 其中@SpringBootConfiguration 跟进去发现,其就等价于@Configuration,一个是在 Spring Boot 的名称,一起是在 Spring 中的名称。@Configuration 本质上也就是一个@Component,也是一个组件而已。

@EnableAutoConfiguration: 打开 Spring Boot 的自动配置机制 @ComponentScan: 允许程序自动扫描包,扫描当前包及其子包下标注了@Component,@Controller,@Service,@Repository 类并纳入到 spring 容器中进行管理。

@Configuration: @Configuration 标注的类能够被 Spring IoC 容器作为一个定义各种 bean 的配置源。

@SpringBootApplication 可以设置从指定的路径中扫描包,纳入 Spring 容器,也可以设置从 Spring 容器中排除某些类。通过如下几个属性实现:

Class<?>[] exclude() default {}: 根据 class 来排除, 排除特定的类加入 spring 容器,传入参数 value 类型是 class 类型。

String[] excludeName() default {}: 根据 class name 来排除, 排除特定的类加入 spring 容器,传入参数 value 类型是 class 的全类名字符串数组。

String[] scanBasePackages() default {}: 指定扫描包,参数是包名的字符串数组。如果想要注入的类,不属于当前@SpringBootApplication 标注类的子包下,就需要用这个属性手动设置需要扫描包的位置。

Class<?>[] scanBasePackageClasses() default {}: 扫描特定的包,参数类似是 Class 类型数组。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • @SpringBootApplication 的作用是什么
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档