- 简单启动方式
-
public static void main(String[] args) { SpringApplication.run(MySpringConfiguration.class, args); }
- 调试方式启动
-
java -jar myproject-0.0.1-SNAPSHOT.jar --debug
-
-
- 高级启动方式
-
@SpringBootApplication public class App { public static void main( String[] args ) { SpringApplication app=new SpringApplication(App.class); app.setBannerMode(Banner.Mode.OFF); app.run(args); } }
-
-
Web Environment
-
A
SpringApplication
attempts to create the right type ofApplicationContext
on your behalf. The algorithm used to determine aWebApplicationType
is fairly simple:- If Spring MVC is present, an
AnnotationConfigServletWebServerApplicationContext
is used - If Spring MVC is not present and Spring WebFlux is present, an
AnnotationConfigReactiveWebServerApplicationContext
is used - Otherwise,
AnnotationConfigApplicationContext
is used
This means that if you are using Spring MVC and the new
WebClient
from Spring WebFlux in the same application, Spring MVC will be used by default. You can override that easily by callingsetWebApplicationType(WebApplicationType)
.It is also possible to take complete control of the
ApplicationContext
type that is used by callingsetApplicationContextClass(…?)
. - If Spring MVC is present, an
-
-
Accessing Application Arguments
- If you need to access the application arguments that were passed to
SpringApplication.run(…?)
, you can inject aorg.springframework.boot.ApplicationArguments
bean. TheApplicationArguments
interface provides access to both the rawString[]
arguments as well as parsedoption
andnon-option
arguments, as shown in the following example: -
import org.springframework.boot.*; import org.springframework.beans.factory.annotation.*; import org.springframework.stereotype.*; @Component public class MyBean { @Autowired 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"] } }
- If you need to access the application arguments that were passed to
-
Using the ApplicationRunner or CommandLineRunner
-
If you need to run some specific code once the
SpringApplication
has started, you can implement theApplicationRunner
orCommandLineRunner
interfaces. Both interfaces work in the same way and offer a singlerun
method, which is called just beforeSpringApplication.run(…?)
completes.The
CommandLineRunner
interfaces provides access to application arguments as a simple string array, whereas theApplicationRunner
uses theApplicationArguments
interface discussed earlier. The following example shows aCommandLineRunner
with arun
method: - If several
CommandLineRunner
orApplicationRunner
beans are defined that must be called in a specific order, you can additionally implement theorg.springframework.core.Ordered
interface or use theorg.springframework.core.annotation.Order
annotation. -
import org.springframework.boot.*; import org.springframework.stereotype.*; @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } }
-
相关文章
- 09-30Failure to transfer org.springframework.boot:spring-boot-starter-parent:pom:2.0.1.RELEASE from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempt
- 09-30Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2
- 09-30Spring Boot整合Swagger2
- 09-30spring boot 集成swagger2
- 09-30Spring Boot 入门系列(二十二)使用Swagger2构建 RESTful API文档
- 09-30Spring-Boot-2-x使用Swagger2构建强大的API文档(一)
- 09-30Spring Boot Swagger2自动生成接口文档和Mock模拟数据
- 09-30Spring Boot 集成swagger2
- 09-30spring boot整合Thymeleaf
- 09-30Spring Boot中的JSTL支持