浅谈SpringBoot核心注解原理

2023-05-16

SpringBoot核心注解原理

今天跟大家来探讨下SpringBoot的核心注解@SpringBootApplication以及run方法,理解下springBoot为什么不需要XML,达到零配置

首先我们先来看段代码


@SpringBootApplication
public class StartEurekaApplication
{
    public static void main(String[] args)
    {
        SpringApplication.run(StartEurekaApplication.class, args);
    }
}  

我们点进@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 {

}  

上面的元注解我们在这里不在做解释,相信大家在开发当中肯定知道,我们要来说@SpringBootConfiguration @EnableAutoConfiguration 这两个注解,到这里我们知道 SpringBootApplication注解里除了元注解,我们可以看到又是@SpringBootConfiguration,@EnableAutoConfiguration,@ComponentScan的组合注解,官网上也有详细说明,那我们现在把目光投向这三个注解。

首先我们先来看 @SpringBootConfiguration,那我们点进来看


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Configuration
public @interface SpringBootConfiguration {

}  

我们可以看到这个注解除了元注解以外,就只有一个@Configuration,那也就是说这个注解相当于@Configuration,所以这两个注解作用是一样的,那他是干嘛的呢,相信很多人都知道,它是让我们能够去注册一些额外的Bean,并且导入一些额外的配置。那@Configuration还有一个作用就是把该类变成一个配置类,不需要额外的XML进行配置。所以@SpringBootConfiguration就相当于@Configuration。

那我们继续来看下一个@EnableAutoConfiguration,这个注解官网说是 让Spring自动去进行一些配置,那我们点进来看


@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
}  

可以看到它是由 @AutoConfigurationPackage,@Import(EnableAutoConfigurationImportSelector.class)这两个而组成的,我们先说@AutoConfigurationPackage,他是说:让包中的类以及子包中的类能够被自动扫描到spring容器中。 我们来看@Import(EnableAutoConfigurationImportSelector.class)这个是核心,之前我们说自动配置,那他到底帮我们配置了什么,怎么配置的?就和@Import(EnableAutoConfigurationImportSelector.class)息息相关,程序中默认使用的类就自动帮我们找到。我们来看EnableAutoConfigurationImportSelector.class


public class EnableAutoConfigurationImportSelector
      extends AutoConfigurationImportSelector {

   @Override
   protected boolean isEnabled(AnnotationMetadata metadata) {
      if (getClass().equals(EnableAutoConfigurationImportSelector.class)) {
         return getEnvironment().getProperty(
               EnableAutoConfiguration.ENABLED_OVERRIDE_PROPERTY, Boolean.class,
               true);
      }
      return true;
   }

}  

可以看到他继承了AutoConfigurationImportSelector我们继续来看AutoConfigurationImportSelector,这个类有一个方法


public String[] selectImports(AnnotationMetadata annotationMetadata) {
   if (!isEnabled(annotationMetadata)) {
      return NO_IMPORTS;
   }
   try {
      AutoConfigurationMetadata autoConfigurationMetadata = AutoConfigurationMetadataLoader
            .loadMetadata(this.beanClassLoader);
      AnnotationAttributes attributes = getAttributes(annotationMetadata);
      List<String> configurations = getCandidateConfigurations(annotationMetadata,
            attributes);
      configurations = removeDuplicates(configurations);
      configurations = sort(configurations, autoConfigurationMetadata);
      Set<String> exclusions = getExclusions(annotationMetadata, attributes);
      checkExcludedClasses(configurations, exclusions);
      configurations.removeAll(exclusions);
      configurations = filter(configurations, autoConfigurationMetadata);
      fireAutoConfigurationImportEvents(configurations, exclusions);
      return configurations.toArray(new String[configurations.size()]);
   }
   catch (IOException ex) {
      throw new IllegalStateException(ex);
   }
}  

这个类会帮你扫描那些类自动去添加到程序当中。我们可以看到getCandidateConfigurations()这个方法,他的作用就是引入系统已经加载好的一些类,到底是那些类呢,我们点进去看一下


protected List<String> getCandidateConfigurations(AnnotationMetadata metadata,
      AnnotationAttributes attributes) {
   List<String> configurations = SpringFactoriesLoader.loadFactoryNames(
         getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader());
   Assert.notEmpty(configurations,
         "No auto configuration classes found in META-INF/spring.factories. If you "
               + "are using a custom packaging, make sure that file is correct.");
   return configurations;
}  

这个类回去寻找的一个目录为META-INF/spring.factories,也就是说他帮你加载让你去使用也就是在这个META-INF/spring.factories目录装配的,他在哪里?

image

我们点进spring.factories来看 

 image

我们可以发现帮我们配置了很多类的全路径,比如你想整合activemq,或者说Servlet image

可以看到他都已经帮我们引入了进来,我看随便拿几个来看


org.springframework.boot.autoconfigure.security.SecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.SecurityFilterAutoConfiguration,\
org.springframework.boot.autoconfigure.security.FallbackWebSecurityAutoConfiguration,\
org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration,\  

比如我们经常用的security,可以看到已经帮你配置好,所以我们的EnableAutoConfiguration主要作用就是让你自动去配置,但并不是所有都是创建好的,是根据你程序去进行决定。 那我们继续来看

@ComponentScan(excludeFilters = { @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class), @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })

这个注解大家应该都不陌生,扫描包,放入spring容器,那他在springboot当中做了什么策略呢?我们可以点跟烟去思考,帮我们做了一个排除策略,他在这里结合SpringBootConfiguration去使用,为什么是排除,因为不可能一上来全部加载,因为内存有限。

那么我们来总结下@SpringbootApplication:就是说,他已经把很多东西准备好,具体是否使用取决于我们的程序或者说配置,那我们到底用不用?那我们继续来看一行代码


public static void main(String[] args)
{
    SpringApplication.run(StartEurekaApplication.class, args);
}  

那们来看下在执行run方法到底有没有用到哪些自动配置的东西,比如说内置的Tomcat,那我们来找找内置Tomcat,我们点进run


public static ConfigurableApplicationContext run(Object[] sources, String[] args) {
        return new SpringApplication(sources).run(args);
    }  

然后他调用又一个run方法,我们点进来看


public ConfigurableApplicationContext run(String... args) {
   //计时器
   StopWatch stopWatch = new StopWatch();
   stopWatch.start();
   ConfigurableApplicationContext context = null;
   FailureAnalyzers analyzers = null;
   configureHeadlessProperty();
   //监听器
   SpringApplicationRunListeners listeners = getRunListeners(args);
   listeners.starting();
   try {
      ApplicationArguments applicationArguments = new DefaultApplicationArguments(
            args);
      ConfigurableEnvironment environment = prepareEnvironment(listeners,
            applicationArguments);
      Banner printedBanner = printBanner(environment);
      //准备上下文
      context = createApplicationContext();
      analyzers = new FailureAnalyzers(context);
         //预刷新context
      prepareContext(context, environment, listeners, applicationArguments,
            printedBanner);
     //刷新context
      refreshContext(context);
     //刷新之后的context
      afterRefresh(context, applicationArguments);
      listeners.finished(context, null);
      stopWatch.stop();
      if (this.logStartupInfo) {
         new StartupInfoLogger(this.mainApplicationClass)
               .logStarted(getApplicationLog(), stopWatch);
      }
      return context;
   }
   catch (Throwable ex) {
      handleRunFailure(context, listeners, analyzers, ex);
      throw new IllegalStateException(ex);
   }
}  

那我们关注的就是 refreshContext(context); 刷新context,我们点进来看

 


private void refreshContext(ConfigurableApplicationContext context) {
   refresh(context);
   if (this.registerShutdownHook) {
      try {
         context.registerShutdownHook();
      }
      catch (AccessControlException ex) {
         // Not allowed in some environments.
      }
   }
}  

我们继续点进refresh(context);


protected void refresh(ApplicationContext applicationContext) {
   Assert.isInstanceOf(AbstractApplicationContext.class, applicationContext);
   ((AbstractApplicationContext) applicationContext).refresh();
}  

他会调用 ((AbstractApplicationContext) applicationContext).refresh();方法,我们点进来看

 


public void refresh() throws BeansException, IllegalStateException {
   synchronized (this.startupShutdownMonitor) {
      // Prepare this context for refreshing.
      prepareRefresh();

      // Tell the subclass to refresh the internal bean factory.
      ConfigurableListableBeanFactory beanFactory = obtainFreshBeanFactory();

      // Prepare the bean factory for use in this context.
      prepareBeanFactory(beanFactory);

      try {
         // Allows post-processing of the bean factory in context subclasses.
         postProcessBeanFactory(beanFactory);

         // Invoke factory processors registered as beans in the context.
         invokeBeanFactoryPostProcessors(beanFactory);

         // Register bean processors that intercept bean creation.
         registerBeanPostProcessors(beanFactory);

         // Initialize message source for this context.
         initMessageSource();

         // Initialize event multicaster for this context.
         initApplicationEventMulticaster();

         // Initialize other special beans in specific context subclasses.
         onRefresh();

         // Check for listener beans and register them.
         registerListeners();

         // Instantiate all remaining (non-lazy-init) singletons.
         finishBeanFactoryInitialization(beanFactory);

         // Last step: publish corresponding event.
         finishRefresh();
      }

      catch (BeansException ex) {
         if (logger.isWarnEnabled()) {
            logger.warn("Exception encountered during context initialization - " +
                  "cancelling refresh attempt: " + ex);
         }

         // Destroy already created singletons to avoid dangling resources.
         destroyBeans();

         // Reset 'active' flag.
         cancelRefresh(ex);

         // Propagate exception to caller.
         throw ex;
      }

      finally {
         // Reset common introspection caches in Spring's core, since we
         // might not ever need metadata for singleton beans anymore...
         resetCommonCaches();
      }
   }
}  

这点代码似曾相识啊 没错,就是一个spring的bean的加载过程我在,解析springIOC加载过程的时候介绍过这里面的方法,如果你看过Spring源码的话 ,应该知道这些方法都是做什么的。现在我们不关心其他的,我们来看一个方法叫做 onRefresh();方法

 


protected void onRefresh() throws BeansException {
   // For subclasses: do nothing by default.
}  

他在这里并没有实现,但是我们找他的其他实现,我们来找 image

我们既然要找Tomcat那就肯定跟web有关,我们可以看到有个ServletWebServerApplicationContext


@Override
protected void onRefresh() {
   super.onRefresh();
   try {
      createWebServer();
   }
   catch (Throwable ex) {
      throw new ApplicationContextException("Unable to start web server", ex);
   }
}  

我们可以看到有一个createWebServer();方法他是创建web容器的,而Tomcat不就是web容器,那他是怎么创建的呢,我们继续看


private void createWebServer() {
   WebServer webServer = this.webServer;
   ServletContext servletContext = getServletContext();
   if (webServer == null && servletContext == null) {
      ServletWebServerFactory factory = getWebServerFactory();
      this.webServer = factory.getWebServer(getSelfInitializer());
   }
   else if (servletContext != null) {
      try {
         getSelfInitializer().onStartup(servletContext);
      }
      catch (ServletException ex) {
         throw new ApplicationContextException("Cannot initialize servlet context",
               ex);
      }
   }
   initPropertySources();
}  

factory.getWebServer(getSelfInitializer());他是通过工厂的方式创建的


public interface ServletWebServerFactory {

   WebServer getWebServer(ServletContextInitializer... initializers);

}  

可以看到 它是一个接口,为什么会是接口。因为我们不止是Tomcat一种web容器。

image

我们看到还有Jetty,那我们来看TomcatServletWebServerFactory


@Override
public WebServer getWebServer(ServletContextInitializer... initializers) {
   Tomcat tomcat = new Tomcat();
   File baseDir = (this.baseDirectory != null) ? this.baseDirectory
         : createTempDir("tomcat");
   tomcat.setBaseDir(baseDir.getAbsolutePath());
   Connector connector = new Connector(this.protocol);
   tomcat.getService().addConnector(connector);
   customizeConnector(connector);
   tomcat.setConnector(connector);
   tomcat.getHost().setAutoDeploy(false);
   configureEngine(tomcat.getEngine());
   for (Connector additionalConnector : this.additionalTomcatConnectors) {
      tomcat.getService().addConnector(additionalConnector);
   }
   prepareContext(tomcat.getHost(), initializers);
   return getTomcatWebServer(tomcat);
}  

那这块代码,就是我们要寻找的内置Tomcat,在这个过程当中,我们可以看到创建Tomcat的一个流程。因为run方法里面加载的东西很多,所以今天就浅谈到这里。如果不明白的话, 我们在用另一种方式来理解下,大家要应该都知道stater举点例子


<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-freemarker</artifactId>
</dependency>  

所以我们不防不定义一个stater来理解下,我们做一个需求,就是定制化不同的人跟大家说你们好,我们来看


<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.4.RELEASE</version>
    <relativePath/>
</parent>
<groupId>com.zgw</groupId>
<artifactId>gw-spring-boot-srater</artifactId>
<version>1.0-SNAPSHOT</version>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-autoconfigure</artifactId>
    </dependency>
</dependencies>  

我们先来看maven配置写入版本号,如果自定义一个stater的话必须依赖spring-boot-autoconfigure这个包,我们先看下项目目录 

 image


public class GwServiceImpl  implements GwService{
    @Autowired
    GwProperties properties;

    @Override
    public void Hello()
    {
        String name=properties.getName();
        System.out.println(name+"说:你们好啊");
    }
}  

我们做的就是通过配置文件来定制name这个是具体实现,


@Component
@ConfigurationProperties(prefix = "spring.gwname")
public class GwProperties {

    String name="zgw";

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}  

这个类可以通过@ConfigurationProperties读取配置文件


@Configuration
@ConditionalOnClass(GwService.class)  //扫描类
@EnableConfigurationProperties(GwProperties.class) //让配置类生效
public class GwAutoConfiguration {

    /**
    * 功能描述 托管给spring
    * @author zgw
    * @return
    */
    @Bean
    @ConditionalOnMissingBean
    public GwService gwService()
    {
        return new GwServiceImpl();
    }
}  

这个为配置类,为什么这么写因为,spring-boot的stater都是这么写的,我们可以参照他仿写stater,以达到自动配置的目的,然后我们在通过spring.factories也来进行配置


org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.gw.GwAutoConfiguration  

然后这样一个简单的stater就完成了,然后可以进行maven的打包,在其他项目引入就可以使用,在这里列出代码地址

https://github.com/zgw1469039806/gwspringbootsrater

到此本文结束,欢迎转载,请标注出处和作者,谢谢!

转载于:https://www.cnblogs.com/zgwjava/p/10892810.html

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

浅谈SpringBoot核心注解原理 的相关文章

随机推荐

  • 电子邮件无法登陆服务器密码,登录到 Exchange ActiveSync 邮件服务器(EAS): 您输入的用户名或密码不起作用。请再次尝试键入。...

    您好 xff0c 我们这边也碰到这个问题 outlook2016连接exchange2013服务器 1 outlook2016添加邮箱 xff0c 选择高级手动配置 xff0c 提示此报错 2 尝试自动引导自动配置 xff0c 但是默认会应
  • idea永久激活使用

    原文 xff1a https www cnblogs com dfsu p 11362594 html 先感谢大佬一波 xff0c 亲测可用 xff0c 小伙伴们按照博客上面操作就行 声明 支持知识产权 xff0c 支持正版产权 xff0c
  • 电信企业文化

    1 2017年 xff0c 中国电信资产规模超过 xff08 A xff09 亿元人民币 xff0c 年收入规模超过4100亿元人民币 A 8000 B 8500 C 9000 D 9500 2 中国电信以党的建设统领企业发展全局 xff0
  • easyui datagrid的行编辑器editor 如何实现新增时可修改,编辑时,不可修改

    项目出现一个需求 xff0c 要求用户界面的用户名 xff0c 新增时 xff0c 可自由输入 xff0c 编辑时 xff0c 不可修改 html页面 lt table id 61 34 gridlist 34 data bind 61 3
  • easyui datagrid的editor编辑器如何为validatebox控件添加改变事件

    项目中需要为行编辑器Editor的某个列的文本框添加改变事件 需求 xff1a 新增行时 xff0c 为用户名输入特殊字符进行验证 xff0c 不允许保存用户数据 html页面 lt table id 61 34 gridlist 34 d
  • 中标麒麟系统远程桌面连接

    中标麒麟是中国自主研发的操作系统 逃 搭载了龙芯 xff08 mips xff09 的中标麒麟OS使用起来非常流畅 xff08 逃 远程连接需要的两个软件 Xmanager amp Xshell 使用起来还是可以的 首先要在麒麟系统上开启s
  • ubuntu 环境变量修改和恢复总结

    通常设置环境变量有三种方法 xff1a 一 临时设置 export JAVA HOME 61 usr lib jvm java 1 7 0 openjdk amd64 export PATH 61 JAVA HOME PATH 二 当前用户
  • Go——反射规则

    反射规则 Value Type和类型实例之间的相互转化如下图 xff1a 1 反射API 从实例到Value 通过实例获取Value对象 xff0c 直接使用reflect ValueOf 函数 例如 xff1a span class to
  • 转载 matlab矩阵数组常用操作

    一 length 返回矩阵最长维的的长度 ndims 返回维数 numel 返回矩阵元素个数 size 返回每一维的长度 xff0c rows cols 61 size A 矩阵块操作 1 repmat 数组块状复制 2 blkdiag 对
  • 微微一笑很倾城(2)

    微微一笑很倾城 正文 第30章 组队前 xff0c 雷神妮妮想死 组队后 xff0c 看到队伍里那一排ID xff0c 雷神妮妮瞬间回光返照HP全满了 就像老话说的那样 xff0c 一个妮妮被雷劈了 xff0c 千万个妮妮在电闪雷鸣中站起来
  • Get the Job You Want(大学英语综合教程4课文)

    UNIT3 1 Harvey Mackay who runs his own company often interviews applicants for jobs Here he lets us into the secret of w
  • Debian9.5 VNC Server远程桌面配置

    VNC概述 VNC Virtual Network Console 是虚拟网络控制台的缩写 VNC 是一款优秀的远程控制工具软件 xff0c 由著名的 AT amp T 的欧洲研究实验室开发的 VNC 是在基于 UNIX 和 Linux操作
  • 嵌入式常用的英文缩写词汇

    原文地址 xff1a https wenku baidu com view 9d4051f4700abb68a982fb4e html 嵌入式常见英文缩写和英文词汇 搜集中 英文缩写 ARM xff1a Advanced RISC Mach
  • 先安装VS2017再安装VS2015遇到的CMake问题

    先安装了VS2017 xff0c 后来有需求安装VS2015 xff0c 安装VS2015的时候遇到下图问题 xff0c 但是控制面板里面看不到Microsoft Visual C 43 43 2015 Redistributable的项目
  • 粒子群算法优化BP生物能神经网络

    定义 xff1a 粒子群中每个粒子的位置表示BP神经网络当前迭代中权值的集合 xff0c 每个粒子的维数由网络中起连接作用的权值的数量和阈值个数决定 xff0c 以给定训练样本集的神经网络输出误差作为神经网络训练问题的适应度函数 xff0c
  • 实现分布式锁技术:Redisson

    1 需求 Spring分布式项目涉及到定时任务 xff0c 目前解决方案 xff1a xff08 1 xff09 集成quartz xff1b xff08 2 xff09 集成redisson xff0c 由于集成quartz需要涉及到数据
  • nginx支持websocket及websocket部分原理介绍

    nginx支持websocket及websocket部分原理介绍 最近ipc通过websocket与server进行通行 xff0c 经过无法通过nginx进行反向代理 xff0c 只有直连nodejs端口 而且部署到阿里云用了slb之后同
  • Windows下分布式环境搭建以及简单测试

    环境配置 xff1a 解压文件 xff1a Nginx服务器和Tomcat服务器 Tomcat服务器配置 xff1a xff08 conf server xml xff09 Nginx配置 xff1a xff08 conf nginx co
  • Go——Inject库

    1 依赖注入和控制反转 在介绍inject之前先简单介绍 依赖注入 和 控制反转 的概念 正常情况下 xff0c 对函数或方法的调用是调用方的主动直接行为 xff0c 调用方清楚地知道被调的函数名是什么 xff0c 参数有哪些类型 xff0
  • 浅谈SpringBoot核心注解原理

    SpringBoot核心注解原理 今天跟大家来探讨下SpringBoot的核心注解 64 SpringBootApplication以及run方法 xff0c 理解下springBoot为什么不需要XML xff0c 达到零配置 首先我们先