您的位置:首页 > 编程语言 > Java开发

spring boot自动配置与启动流程分析

2018-02-03 16:36 1126 查看

spring boot自动配置与启动流程分析

 我们先了解一下spring的注解@Controller、@Repository、@Service与@Component的关系:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface Controller {

/**
* The value may indicate a suggestion for a logical component name,
* to be turned into a Spring bean in case of an autodetected component.
* @return the suggested component name, if any (or empty String otherwise)
*/
@AliasFor(annotation = Component.class)
String value() default "";

}


  @Controller、@Repository以及@Service的注解都是一样的,其上都有@Component,目前spring版本对这个几个个注解的功能没区别,都@Component的别名。其实我们可以自己定义注解,如:

@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MyController {

}
 
 这样带有注解
@MyController的类也可以被spring实例化,放到spring容器中托管。其实spring boot也是类似手法。

 spring boot的注解@SpringBootApplication、@SpringBootConfiguration、@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 {};

}
 
 @EnableAutoConfiguration就有组合注解@AutoConfigurationPackage。这些注解的功能大多依赖 spring
的@Import,此注解上的类表示其注解功能的实现。现在我们看看spring boot的自动配置在启动流程哪些关键之处起作用的。

  经debug,整理了一些spring boot 启动的流程(并没有很全面):空间太小了,图画的不是很好:



     在线版:http://on-img.com/chart_image/5a600665e4b010a6e720bf6b.png
或git地址。(右键新标签打开该图片可以放大查看)
    图上其实都加上了部分注解,下面拿些我认为重要的说一下:

package com.sdcuike.springboot.practice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
* @author sdcuike
* @date 2018/01/17
* @since 2018/1/17
*/
@SpringBootApplication
public class SpringApplicationBoot {
public static void main(String[] args) {

SpringApplication.run(SpringApplicationBoot.class, args);

}
}
   上面几行代码加一个注解@SpringBootApplication,就可以启动spring boot 应用,代码

SpringApplication.run(SpringApplicationBoot.class, args);

   主要实现的功能如图所示:

   


 

      其中包括了spring 容器的初始化功能。功能4表示根据判断的应用类型创建相应的spring 容器,在作者这里是web应用,所以是AnnotationConfigServletWebServerApplicationContext,此容器默认的构造函数实现的功能如下图所示:



这个容器主要注册和注解相关的post processors实例,大部分都是BeanFactoryPostProcessor及BeanPostProcessor的子类。主要处理spring的注解@Configuration、 @Autowired、@Value、@Injec、 @Required、 @PostConstruct、@PreDestroy 、
 @EventListener等等。其中ConfigurationClassPostProcessor 处理 带有注解@Configuration的类。
这些和注解相关的post processors实例的功能处理主要由org.springframework.context.support
.AbstractApplicationContext#invokeBeanFactoryPostProcessors调用:



功能如上图所示,而且spring boot的注解@EnableAutoConfiguration中的
@Import(AutoConfigurationImportSelector.class)开始加载spring boot约定好的自动配置类。以后的流程是经典的spring容器实例化类的流程,不在叙述了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: