spring- boot 笔记

java -jar 命令一般用于生产环境, mvn spring-boot:run 用于开发阶段

Spring Boot Plugin有如下目标:

1.spring-boot:run  运行Spring Boot应用

2.spring-boot:repackage 重新打包jar/war包为可执行包

3.spring-boot:start 和spring-boot:stop 管理Spring Boot 应用的生命周期(即集成测试)



spring-boot:help | 展示spring-boot-maven-plugin的帮助信息,使用 mvn spring-boot:help -Ddetail=true -Dgoal=<goal-name>展示参数详情 |
spring-boot:repackage | 重新打包存在的jar或者war包从而使他们可以在命令行使用jar -jar来执行,使用layout=NONE也可以简单的打包有嵌套依赖的jar(没有主类,所以无法执行)
spring-boot:run | 运行一个可执行的应用 |
spring-boot:start | 启动Spring应用程序。和run目标不同,该目标不会阻塞,并且允许其他目标来操作应用程序。这个目标通常是在应用程序集成测试套件开始之前和停止之后的继承测试脚本中使用
spring-boot:stop | 停止使用start目标启动的spring应用程序,通常在测试套件完成后被调用。


 

Spring Boot Maven plugin的5个Goals

  • spring-boot:repackage,默认goal。在mvn package之后,再次打包可执行的jar/war,同时保留mvn package生成的jar/war为.origin
  • spring-boot:run,运行Spring Boot应用
  • spring-boot:start,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
  • spring-boot:stop,在mvn integration-test阶段,进行Spring Boot应用生命周期的管理
  • spring-boot:build-info,生成Actuator使用的构建信息文件build-info.properties
layout属性的值可以如下:

JAR,即通常的可执行jar
    Main-Class: org.springframework.boot.loader.JarLauncher

WAR,即通常的可执行war,需要的servlet容器依赖位于WEB-INF/lib-provided
    Main-Class: org.springframework.boot.loader.warLauncher

ZIP,即DIR,类似于JAR
    Main-Class: org.springframework.boot.loader.PropertiesLauncher

MODULE,将所有的依赖库打包(scope为provided的除外),但是不打包Spring Boot的任何Launcher
    NONE,将所有的依赖库打包,但是不打包Spring Boot的任何Launcher

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 相关源码:

//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));
    this.webApplicationType = WebApplicationType.deduceFromClasspath();
    setInitializers((Collection) getSpringFactoriesInstances(ApplicationContextInitializer.class));
    setListeners((Collection) getSpringFactoriesInstances(ApplicationListener.class));
    this.mainApplicationClass = deduceMainApplicationClass();
}

//org.springframework.boot.SpringApplication#run(java.lang.String...)
public ConfigurableApplicationContext run(String... args) {
    SpringApplicationRunListeners listeners = getRunListeners(args);
    listeners.starting();
    try {
        ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
        ConfigurableEnvironment environment = prepareEnvironment(listeners, applicationArguments);
        configureIgnoreBeanInfo(environment);
        Banner printedBanner = printBanner(environment);
        context = createApplicationContext();
        exceptionReporters = getSpringFactoriesInstances(SpringBootExceptionReporter.class,
                new Class[] { ConfigurableApplicationContext.class }, context);
        prepareContext(context, environment, listeners, applicationArguments, printedBanner);
        refreshContext(context);
        afterRefresh(context, applicationArguments);
        if (this.logStartupInfo) {
            new StartupInfoLogger(this.mainApplicationClass).logStarted(getApplicationLog(), stopWatch);
        }
        listeners.started(context);
        callRunners(context, applicationArguments);
    }
        listeners.running(context);

    return context;
}

private void prepareContext(ConfigurableApplicationContext context, ConfigurableEnvironment environment,
        SpringApplicationRunListeners listeners, ApplicationArguments applicationArguments, Banner printedBanner) {
    context.setEnvironment(environment);
    postProcessApplicationContext(context);
    applyInitializers(context);
    listeners.contextPrepared(context);
    if (this.logStartupInfo) {
        logStartupInfo(context.getParent() == null);
        logStartupProfileInfo(context);
    }
    // Add boot specific singleton beans
    ConfigurableListableBeanFactory beanFactory = context.getBeanFactory();
    beanFactory.registerSingleton("springApplicationArguments", applicationArguments);
    if (printedBanner != null) {
        beanFactory.registerSingleton("springBootBanner", printedBanner);
    }
    if (beanFactory instanceof DefaultListableBeanFactory) {
        ((DefaultListableBeanFactory) beanFactory)
                .setAllowBeanDefinitionOverriding(this.allowBeanDefinitionOverriding);
    }
    if (this.lazyInitialization) {
        context.addBeanFactoryPostProcessor(new LazyInitializationBeanFactoryPostProcessor());
    }
    // Load the sources
    Set<Object> sources = getAllSources();
    Assert.notEmpty(sources, "Sources must not be empty");
    load(context, sources.toArray(new Object[0]));
    listeners.contextLoaded(context);
}

 

 

 

 

 

资料来源于王富强<springBoot 解密>

 

posted @ 2020-03-07 16:59  龘人上天  阅读(261)  评论(0编辑  收藏  举报