Spring boot 梳理 - SpringApplication
- 简单启动方式
-
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
SpringApplicationattempts to create the right type ofApplicationContexton your behalf. The algorithm used to determine aWebApplicationTypeis fairly simple:- If Spring MVC is present, an
AnnotationConfigServletWebServerApplicationContextis used - If Spring MVC is not present and Spring WebFlux is present, an
AnnotationConfigReactiveWebServerApplicationContextis used - Otherwise,
AnnotationConfigApplicationContextis used
This means that if you are using Spring MVC and the new
WebClientfrom 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
ApplicationContexttype 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.ApplicationArgumentsbean. TheApplicationArgumentsinterface provides access to both the rawString[]arguments as well as parsedoptionandnon-optionarguments, 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
SpringApplicationhas started, you can implement theApplicationRunnerorCommandLineRunnerinterfaces. Both interfaces work in the same way and offer a singlerunmethod, which is called just beforeSpringApplication.run(…)completes.The
CommandLineRunnerinterfaces provides access to application arguments as a simple string array, whereas theApplicationRunneruses theApplicationArgumentsinterface discussed earlier. The following example shows aCommandLineRunnerwith arunmethod: - If several
CommandLineRunnerorApplicationRunnerbeans are defined that must be called in a specific order, you can additionally implement theorg.springframework.core.Orderedinterface or use theorg.springframework.core.annotation.Orderannotation. -
import org.springframework.boot.*; import org.springframework.stereotype.*; @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... } }
-
-
Admin Features
- It is possible to enable admin-related features for the application by specifying the
spring.application.admin.enabledproperty. This exposes theSpringApplicationAdminMXBeanon the platformMBeanServer. You could use this feature to administer your Spring Boot application remotely. This feature could also be useful for any service wrapper implementation. - If you want to know on which HTTP port the application is running, get the property with a key of
local.server.port. -
Take care when enabling this feature, as the MBean exposes a method to shutdown the application.
- It is possible to enable admin-related features for the application by specifying the
-
Application Exit
-
Each
SpringApplicationregisters a shutdown hook with the JVM to ensure that theApplicationContextcloses gracefully on exit. All the standard Spring lifecycle callbacks (such as theDisposableBeaninterface or the@PreDestroyannotation) can be used.In addition, beans may implement the
org.springframework.boot.ExitCodeGeneratorinterface if they wish to return a specific exit code whenSpringApplication.exit()is called. This exit code can then be passed toSystem.exit()to return it as a status code, as shown in the following example: - Also, the
ExitCodeGeneratorinterface may be implemented by exceptions. When such an exception is encountered, Spring Boot returns the exit code provided by the implementedgetExitCode()method. -
@SpringBootApplication public class ExitCodeApplication { @Bean public ExitCodeGenerator exitCodeGenerator() { return () -> 42; } public static void main(String[] args) { System.exit(SpringApplication .exit(SpringApplication.run(ExitCodeApplication.class, args))); } }
-
浙公网安备 33010602011771号