美文网首页
02 开始SpringBoot

02 开始SpringBoot

作者: Tayle | 来源:发表于2019-04-18 15:51 被阅读0次

02 开始SpringBoot

创建SpringBoot项目

​ 通过sts新建一个SpringBoot项目,可以由Spring官网生成,如下图所示:

1555410649660.png

​ Next选择需要的Starter,也可以不选择,创建完成后在pom.xml中添加对应的dependency。

常规SpringBoot项目对应的pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

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

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

启动SpringBoot项目

​ 创建好项目后会看到自动生成了一个以Application结尾的类,其中有一个main方法执行SpringApplication.run方法。

​ 启动方式一:当做一个普通的java项目直接运行main方法。

​ 启动方式二:依赖ADE工具,右键项目run as Spring Boot App。

​ 启动方式三:切换到项目所在路径,执行命令mvn spring-boot:run

​ 启动方式四:mvn编译好后,直接java -jar ****.jar

SpringBoot启动加载原理

​ 总入口类及main方法如下所示,从下面代码可以看出SpringBoot的神秘面纱,主要是注解SpringBootApplication与SpringApplication类。

@SpringBootApplication
public class YiMoApplication {

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

注解SpringBootApplication简解

​ 查看注解源代码可以看到该注解定义了多个注解。

@Target(ElementType.TYPE)             
@Retention(RetentionPolicy.RUNTIME)   
@Documented                           
@Inherited                            
@SpringBootConfiguration              
@EnableAutoConfiguration              
@ComponentScan(excludeFilters = {  //排除TypeExcludeFilter与AutoConfigurationExcludeFilter
        @Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
        @Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
    ....
}

其中@Target,@Retention,@Documented,@Inherited均为jdk的元注解

SpringBootConfiguration,通过源码可以看到该注解只集成了@Configuration,标示启动类也是IOC容器中基于JavaConfig的配置类。

EnableAutoConfiguration,借助@Import的帮助,将所有符合自动配置条件的bean定义加载到IoC容器。

ComponentScan,Spring扫描加载组件,默认为当前类所在包下所有带有@Component注解或继承@Component的所有组件类。

Run方法执行流程分析

​ 通过查看源码可以了解到在创建SpringApplication对象时初始化了一系列对象。

public SpringApplication(ResourceLoader resourceLoader, Class<?>... primarySources) {
        this.resourceLoader = resourceLoader;
        Assert.notNull(primarySources, "PrimarySources must not be null");
        this.primarySources = new LinkedHashSet<>(Arrays.asList(primarySources));
        //java webApplicationType标识是否为web应用,web分为基于reaction与servlet。
        this.webApplicationType = WebApplicationType.deduceFromClasspath();
        setInitializers((Collection) getSpringFactoriesInstances(
                ApplicationContextInitializer.class));
        setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    //通过stackTrace(堆栈轨迹)初始化mainApplicationClass
        this.mainApplicationClass = deduceMainApplicationClass();
    }

getSpringFactoriesInstances()//加载META-INF/spring.factories文件中的所有Application Context Initializers。

org.springframework.context.ApplicationContextInitializer=\
org.springframework.boot.context.ConfigurationWarningsApplicationContextInitializer,\
org.springframework.boot.context.ContextIdApplicationContextInitializer,\
org.springframework.boot.context.config.DelegatingApplicationContextInitializer,\
org.springframework.boot.web.context.ServerPortInfoApplicationContextInitializer
SpringBootApplicationContextInitializer.png

getSpringFactoriesInstances()//加载META-INF/spring.factories文件中所有ApplicationListener

# Application Listeners
org.springframework.context.ApplicationListener=\
org.springframework.boot.ClearCachesApplicationListener,\
org.springframework.boot.builder.ParentContextCloserApplicationListener,\
org.springframework.boot.context.FileEncodingApplicationListener,\
org.springframework.boot.context.config.AnsiOutputApplicationListener,\
org.springframework.boot.context.config.ConfigFileApplicationListener,\
org.springframework.boot.context.config.DelegatingApplicationListener,\
org.springframework.boot.context.logging.ClasspathLoggingApplicationListener,\
org.springframework.boot.context.logging.LoggingApplicationListener,\
org.springframework.boot.liquibase.LiquibaseServiceLocatorApplicationListener
SpringBootApplicationListener.png

然后执行run方法。

public ConfigurableApplicationContext run(String... args) {
        StopWatch stopWatch = new StopWatch();//工具类,可以用来方便的记录程序的运行时间
        stopWatch.start();
        ConfigurableApplicationContext context = null;
        Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList<>();
        configureHeadlessProperty();//配置headless模型,默认为开启(在缺少显示屏、键盘或者鼠标时的系统配置)
        SpringApplicationRunListeners listeners = getRunListeners(args);
        listeners.starting();//加载运行监听并启动
        try {
            ApplicationArguments applicationArguments = new DefaultApplicationArguments(
                    args);
            ConfigurableEnvironment environment = prepareEnvironment(listeners,
                    applicationArguments);
            configureIgnoreBeanInfo(environment);
            Banner printedBanner = printBanner(environment);//输出banner
            context = createApplicationContext();//加载上下文配置信息
            exceptionReporters = getSpringFactoriesInstances(
                    SpringBootExceptionReporter.class,//加载spring.factories中的Reporter
                    new Class[] { ConfigurableApplicationContext.class }, context);
            //Spring上下文前置处理
            prepareContext(context, environment, listeners, applicationArguments,
                    printedBanner);
            refreshContext(context);
            afterRefresh(context, applicationArguments);
            stopWatch.stop();
            if (this.logStartupInfo) {
                new StartupInfoLogger(this.mainApplicationClass)
                        .logStarted(getApplicationLog(), stopWatch);
            }
            listeners.started(context);
            //上下文都加载完成后调用ApplicationRunner或CommandLineRunner
            callRunners(context, applicationArguments);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, listeners);
            throw new IllegalStateException(ex);
        }
        try {
            listeners.running(context);
        }
        catch (Throwable ex) {
            handleRunFailure(context, ex, exceptionReporters, null);
            throw new IllegalStateException(ex);
        }
        return context;
    }

spring.factories中配置的ApplicationContext与ApplicationListener后续继续阅读源代码分析。

相关文章

网友评论

      本文标题:02 开始SpringBoot

      本文链接:https://www.haomeiwen.com/subject/kbliwqtx.html