资讯专栏INFORMATION COLUMN

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

seanlook / 1146人阅读

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

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

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

</>复制代码

  1. /**
  2. * Indicates a {@link Configuration configuration} class that declares one or more
  3. * {@link Bean @Bean} methods and also triggers {@link EnableAutoConfiguration
  4. * auto-configuration} and {@link ComponentScan component scanning}. This is a convenience
  5. * annotation that is equivalent to declaring {@code @Configuration},
  6. * {@code @EnableAutoConfiguration} and {@code @ComponentScan}.
  7. *
  8. * @author Phillip Webb
  9. * @author Stephane Nicoll
  10. * @since 1.2.0
  11. */
  12. @Target(ElementType.TYPE)
  13. @Retention(RetentionPolicy.RUNTIME)
  14. @Documented
  15. @Inherited
  16. @SpringBootConfiguration
  17. @EnableAutoConfiguration
  18. @ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
  19. @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
  20. public @interface SpringBootApplication

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

@SpringBootConfiguration

</>复制代码

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

  7. * Application should only ever include one {@code @SpringBootConfiguration} and
  8. * most idiomatic Spring Boot applications will inherit it from
  9. * {@code @SpringBootApplication}.
  10. *
  11. * @author Phillip Webb
  12. * @since 1.4.0
  13. */
  14. @Target(ElementType.TYPE)
  15. @Retention(RetentionPolicy.RUNTIME)
  16. @Documented
  17. @Configuration
  18. public @interface SpringBootConfiguration {
  19. }

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

@EnableAutoConfiguration

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

</>复制代码

  1. /**
  2. * Enable auto-configuration of the Spring Application Context, attempting to guess and
  3. * configure beans that you are likely to need. Auto-configuration classes are usually
  4. * applied based on your classpath and what beans you have defined. For example, if you
  5. * have {@code tomcat-embedded.jar} on your classpath you are likely to want a
  6. * {@link TomcatServletWebServerFactory} (unless you have defined your own
  7. * {@link ServletWebServerFactory} bean).
  8. *

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

  12. * Auto-configuration tries to be as intelligent as possible and will back-away as you
  13. * define more of your own configuration. You can always manually {@link #exclude()} any
  14. * configuration that you never want to apply (use {@link #excludeName()} if you don"t
  15. * have access to them). You can also exclude them via the
  16. * {@code spring.autoconfigure.exclude} property. Auto-configuration is always applied
  17. * after user-defined beans have been registered.
  18. *

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

  26. * Auto-configuration classes are regular Spring {@link Configuration} beans. They are
  27. * located using the {@link SpringFactoriesLoader} mechanism (keyed against this class).
  28. * Generally auto-configuration beans are {@link Conditional @Conditional} beans (most
  29. * often using {@link ConditionalOnClass @ConditionalOnClass} and
  30. * {@link ConditionalOnMissingBean @ConditionalOnMissingBean} annotations).
  31. *
  32. * @author Phillip Webb
  33. * @author Stephane Nicoll
  34. * @see ConditionalOnBean
  35. * @see ConditionalOnMissingBean
  36. * @see ConditionalOnClass
  37. * @see AutoConfigureAfter
  38. * @see SpringBootApplication
  39. */
  40. @Target(ElementType.TYPE)
  41. @Retention(RetentionPolicy.RUNTIME)
  42. @Documented
  43. @Inherited
  44. @AutoConfigurationPackage
  45. @Import(AutoConfigurationImportSelector.class)
  46. public @interface EnableAutoConfiguration {
  47. String ENABLED_OVERRIDE_PROPERTY = "spring.boot.enableautoconfiguration";
  48. /**
  49. * Exclude specific auto-configuration classes such that they will never be applied.
  50. * @return the classes to exclude
  51. */
  52. Class[] exclude() default {};
  53. /**
  54. * Exclude specific auto-configuration class names such that they will never be
  55. * applied.
  56. * @return the class names to exclude
  57. * @since 1.3.0
  58. */
  59. String[] excludeName() default {};
  60. }

  当我们启用了@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元查看
<