美文网首页
Spring Boot Document Part.4 23.S

Spring Boot Document Part.4 23.S

作者: TinChiWay | 来源:发表于2017-12-14 22:49 被阅读0次

23.SpringApplication

SpringApplication类使用main()方法来方便的方式来引导Spring application的启动.在许多情况下,你可以委托给静态SpringApplication.run方法:

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

一般的启动失败大都是因为端口(8080)被占用

启动打印的横幅可以自定义

Variable Description
${application.version} The version number of your application as declared in MANIFEST.MF. For example Implementation-Version: 1.0 is printed as 1.0.
${application.formatted-version} The version number of your application as declared in MANIFEST.MF formatted for display (surrounded with brackets and prefixed with v). For example (v1.0).
${spring-boot.version} The Spring Boot version that you are using. For example 1.5.9.RELEASE.
${spring-boot.formatted-version} The Spring Boot version that you are using formatted for display (surrounded with brackets and prefixed with v). For example (v1.5.9.RELEASE).
${Ansi.NAME} (or ${AnsiColor.NAME}, ${AnsiBackground.NAME}, ${AnsiStyle.NAME}) Where NAME is the name of an ANSI escape code. See AnsiPropertySource for details.
${application.title} The title of your application as declared in MANIFEST.MF. For exampleImplementation-Title: MyApp is printed as MyApp.

yaml 可以禁用横幅

spring:
    main:
        banner-mode: "off"

Accessing application arguments

访问应用程序参数

可以注入一个org.springframework.boot.ApplicationArgumentsbean,来访问程序参数.

import org.springframework.boot.*
import org.springframework.beans.factory.annotation.*
import org.springframework.stereotype.*

@Component
public class MyBean {

    @Autowired //注入args[]
    public MyBean(ApplicationArguments args) {
        boolean debug = args.containsOption("debug");
        List<String> files = args.getNonOptionArgs();
        // if run with "--debug logfile.txt" debug=true, files=["logfile.txt"]
    }

}

使用ApplicationRunner或CommandLineRunner

你可以通过实现ApplicationRunnerCommandLineRunner 这两个接口来在启动时运行一些特殊的代码once,这两个接口都以相同的方式工作,并提供一个run方法,将在SpringApplication.run() 完成之前调用.

CommandLineRunner 接口使用一个简单的String[] 提供对应用程序参数的访问,而ApplicationRunner 则使用ApplicationArguments 接口

import org.springframework.boot.*
import org.springframework.stereotype.*

@Component
public class MyBean implements CommandLineRunner {

    public void run(String... args) {
        // Do something...
    }

}

程序退出

如果bean 在SpringApplication.exit() 调用时希望返回一个特定的退出代码,它们可以实现org.springframework.boot.ExitCodeGenerator 接口.这个退出代码然后可以被传递给System.exit()作为状态代码返回它.

@SpringBootApplication
public class ExitCodeApplication {

    @Bean
    public ExitCodeGenerator exitCodeGenerator() {
        return new ExitCodeGenerator() {
            @Override
            public int getExitCode() {
                return 42;
            }
        };
    }

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

}

相关文章

网友评论

      本文标题:Spring Boot Document Part.4 23.S

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