资讯专栏INFORMATION COLUMN

SpringBoot源码分析系列(一)--核心注解

seanlook / 959人阅读

摘要:用于主类上最最最核心的注解,表示这是一个项目,用于开启的各项能力。下面我们来分析一下这个注解的组成以及作用通过上面的代码我们可以看出来是一个组合注解,主要由和这三个注解组成的。通过源码可以看出也是一个组合注解。

  SpringBoot项目一般都会有Application的入口类,入口类中会有main方法,这是一个标准的java应用程序的入口方法。@SpringBootApplication用于Spring主类上最最最核心的注解,表示这是一个SpringBoot项目,用于开启SpringBoot的各项能力。

  下面我们来分析一下@SpringBootApplication这个注解的组成以及作用

/**
 * Indicates a {@link Configuration configuration} class that declares one or more
 * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
 * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
 * annotation that is equivalent to declaring {@code @Configuration},
 * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
 *
 * @author Phillip Webb
 * @author Stephane Nicoll
 * @since 1.2.0
 */
@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

  通过上面的代码我们可以看出来@SpringBootApplication是一个组合注解,主要由@SpringBootConfiguration、@EnableAutoConfiguration和@ComponentScan这三个注解组成的。

@SpringBootConfiguration
/**
 * Indicates that a class provides Spring Boot application
 * {@link Configuration @Configuration}. Can be used as an alternative to the Spring"s
 * standard {@code @Configuration} annotation so that configuration can be found
 * automatically (for example in tests).
 * 

* Application should only ever include one {@code @SpringBootConfiguration} and * most idiomatic Spring Boot applications will inherit it from * {@code @SpringBootApplication}. * * @author Phillip Webb * @since 1.4.0 */ @Target(ElementType.TYPE) @Retention(RetentionPolicy.RUNTIME) @Documented @Configuration public @interface SpringBootConfiguration { }

  通过源码可以看出@SpringBootConfiguration也是一个组合注解。@SpringBootConfiguration是SpringBoot项目的配置注解,标识这是一个被装载的Bean,在SpringBoot项目中推荐使用@SpringBootConfiguration替代@Configuration

@EnableAutoConfiguration

  @EnableAutoConfiguration的作用是开启自动配置功能。以前我们需要配置的东西,SpringBoot帮我们自动配置;@EnableAutoConfiguration告诉SpringBoot开启自动配置功能;这样自动配置才能生效

/**
 * 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. For example, if you
 * have {@code tomcat-embedded.jar} on your classpath you are likely to want a
 * {@link TomcatServletWebServerFactory} (unless you have defined your own
 * {@link ServletWebServerFactory} bean).
 * 

* When using {@link SpringBootApplication}, the auto-configuration of the context is * automatically enabled and adding this annotation has therefore no additional effect. *

* 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 {@link #exclude()} any * configuration that you never want to apply (use {@link #excludeName()} if you don"t * have access to them). You can also exclude them via the * {@code spring.autoconfigure.exclude} property. Auto-configuration is always applied * after user-defined beans have been registered. *

* The package of the class that is annotated with {@code @EnableAutoConfiguration}, * usually via {@code @SpringBootApplication}, has specific significance and is often used * as a "default". For example, it will be used when scanning for {@code @Entity} classes. * It is generally recommended that you place {@code @EnableAutoConfiguration} (if you"re * not using {@code @SpringBootApplication}) in a root package so that all sub-packages * and classes can be searched. *

* Auto-configuration classes are regular Spring {@link Configuration} beans. They are * located using the {@link SpringFactoriesLoader} mechanism (keyed against this class). * Generally auto-configuration beans are {@link Conditional @Conditional} beans (most * often using {@link ConditionalOnClass @ConditionalOnClass} and * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations). * * @author Phillip Webb * @author Stephane Nicoll * @see ConditionalOnBean * @see ConditionalOnMissingBean * @see ConditionalOnClass * @see AutoConfigureAfter * @see SpringBootApplication */ @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 {}; }

  当我们启用了@EnableAutoConfiguration注解,启动自动配置后,该注解会使SpringBoot根据项目中依赖的jar包自动配置项目的配置项。例如:引入了Spring-boot-starter-web的依赖,项目中也就会引入SpringMVC的依赖,SpringBoot就会自动配置tomcat和SpringMVC

@ComponentScan

  @ComponentScan注解会自动扫描包路径下的所有@Controller、@Service、@Repository、@Component的类,不配置包路径的话,SpringBoot中默认扫描@SpringBootApplication注解所在类的同级目录以及子目录下的相关注解。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/75688.html

相关文章

  • springboot源码分析系列(三)--@EnableAutoConfiguration自动配置加

    摘要:常规的配置让开发人员将更多的经历耗费在了配置文件上。其中有三个注解,,。以前我们需要配置的东西,帮我们自动配置,告诉开启自动配置功能,这样自动配置才能生效。 为什么需要自动化配置   在常规的spring应用程序中,充斥着大量的配置文件,我们需要手动去配置这些文件,如配置组件扫描、视图解析器、http编码等等。常规的配置让开发人员将更多的经历耗费在了配置文件上。而这些配置都是一些固定模...

    Travis 评论0 收藏0
  • springboot源码分析系列(三)--@EnableAutoConfiguration自动配置加

    摘要:常规的配置让开发人员将更多的经历耗费在了配置文件上。其中有三个注解,,。以前我们需要配置的东西,帮我们自动配置,告诉开启自动配置功能,这样自动配置才能生效。 为什么需要自动化配置   在常规的spring应用程序中,充斥着大量的配置文件,我们需要手动去配置这些文件,如配置组件扫描、视图解析器、http编码等等。常规的配置让开发人员将更多的经历耗费在了配置文件上。而这些配置都是一些固定模...

    macg0406 评论0 收藏0
  • java篇

    摘要:多线程编程这篇文章分析了多线程的优缺点,如何创建多线程,分享了线程安全和线程通信线程池等等一些知识。 中间件技术入门教程 中间件技术入门教程,本博客介绍了 ESB、MQ、JMS 的一些知识... SpringBoot 多数据源 SpringBoot 使用主从数据源 简易的后台管理权限设计 从零开始搭建自己权限管理框架 Docker 多步构建更小的 Java 镜像 Docker Jav...

    honhon 评论0 收藏0
  • “过时”的SpringMVC我们到底在用什么?深入分析DispatchServlet源码

    摘要:问题来了,我们到底还在用吗答案是,不全用。后者是初始化的配置,主要是的配置。启动类测试启动项目后,在浏览器里面输入。通过查询已装载的,并且支持该而获取的。按照前面对的描述,对于而言,这个必定是。的核心在的方法中。 之前已经分析过了Spring的IOC(《零基础带你看Spring源码——IOC控制反转》)与AOP(《从源码入手,一文带你读懂Spring AOP面向切面编程》)的源码,本次...

    array_huang 评论0 收藏0
  • Java后端

    摘要:,面向切面编程,中最主要的是用于事务方面的使用。目标达成后还会有去构建微服务,希望大家多多支持。原文地址手把手教程优雅的应用四手把手实现后端搭建第四期 SpringMVC 干货系列:从零搭建 SpringMVC+mybatis(四):Spring 两大核心之 AOP 学习 | 掘金技术征文 原本地址:SpringMVC 干货系列:从零搭建 SpringMVC+mybatis(四):Sp...

    joyvw 评论0 收藏0

发表评论

0条评论

seanlook

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<