You can easily start a Spring application with the SpringApplication.run() method, for example
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}Copy the code
The results
. ____ _ __ _ _ / \ \ / ___ '_ __ _ _) (_ _ __ __ _ \ \ \ \ (\ ___ () |' _ | '_ | |' _ \ / _ ` | \ \ \ \ \ \ / ___) | | _) | | | | | || (_| | ) ) ) ) ' |____| .__|_| |_|_| |_\__, | / / / / =========|_|==============|___/=/_/_/_/ :: Spring Boot :: V2.0.1. RELEASE the 00:08:16 2013-07-31. 56603-117 the INFO [main] O.S.B.S.A pp. SampleApplication: Starting SampleApplication V0.1.0 on myComputer with PID 56603 (/apps/ Myapp. jar started by Pwebb) 2013-07-31 00:08:16. 56603-166 the INFO [main] ationConfigServletWebServerApplicationContext: Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@6e5a8246: startup date [Wed Jul 31 00:08:16 PDT 2013]; The root of the context hierarchy 13:09:54 2014-03-04. 41370-912 the INFO [main]. T.T omcatServletWebServerFactory: Server the initialized with the port: 8080 2014-03-04 13:09:56. 41370-501 the INFO [main] O.S.B.S.A pp. SampleApplication: Started SampleApplication in 2.992 seconds (JVM running for 3.658)Copy the code
The default Log level is INFO.
Custom banner
The banner is printed when the Spring application starts. If you want to customize the banner, you can add banner. TXT to your classpath or set the spring.banner.location property to set the path for the banner. The default encoding of the banner is UTF-8. If utF-8 is not used, you can set the encoding of the banner by setting the spring.banner. Charset attribute. In addition to a text file, also can use, banner. GIF, banner, jpeg, banner. PNG image file as a banner, or set up spring. The banner. The image. The location attribute set image banner. Pictures have Spring to ASCII output. Add the following placeholders to banner. TXT
- In ${application. Version}
MANIFEST.MF
Version number defined in, for exampleImplementation - Version: 1.0
The version number is 1.0 - ${application. Formatted -version} Format the version number with V, such as V1.0
- ${spring-boot.version} Specifies the version of Spring Boot to use, for example, 2.0.1.release
- ${spring-boot.formatted-version} Add V to the version number, for example, v2.0.1.RELEASE
- ${ansibackward. NAME},${ansibackward. NAME},${ansistyl. NAME} Ansi escape code NAME
-
${application.title} manifest. MF Specifies the name of the application, for example, implemention-title: MyApp
Also can realize org. Springframework. Boot. The Banner interface printBanner custom Banner () method, using SpringApplication. SetBanner () sets the Banner. Configure spring.main.banner-mode to set whether to display the banner on the console
Custom SpringApplication
You can customize SpringApplication by creating custom SpringApplication examples, for example
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}Copy the code
Customize the SpringApplication with builder
new SpringApplicationBuilder()
.sources(Parent.class)
.child(Application.class)
.bannerMode(Banner.Mode.OFF)
.run(args);Copy the code
Apply events and listeners
In addition to the usual Spring Framework events, such as ContextRefreshedEvent, SpringApplication sends some other application times. Because some events are triggered before the ApplicationContext is created, listeners for these events cannot be registered by @bean. Can use SpringApplication. AddListeners () illegal or SpringApplicationBuilder. Listeners registered listener () method. You can also automatically register listeners by adding a meta-INF/spring.Factories file to your project, For example org. Springframework. Context. The ApplicationListener = internet-service providers. Example. Project. MyListener application starts, application time trigger 1 in the following order. Trigger the ApplicationStartingEvent event when the application starts running. 2. Before the ApplicationContext creating, the Environment is available to trigger ApplicationEnvironmentPreparedEvent events. 3. After the bean definition is loaded, the ApplicationPreparedEvent event is triggered before the refresh. Trigger ApplicationStartedEvent 5. Command-lie runner triggers ApplicationReadyEvent to indicate that the application can accept the request 6. If an exception occurs, the ApplicationFailedEvent application event is triggered through the Spring Framework’s event publishing mechanism, which ensures that events sent to a child context are also sent to its ancestor context. Therefore, if the ApplicationContext is hierarchical, it may receive multiple identical events. To distinguish these events, the application needs to inject its own ApplicationContext, and determine whether to process the layer event when receiving the event. You can inject the ApplicationContext with inheritationContextaware, or you can inject the ApplicationContext with @AutoWired if the listener is on.
Web environment
SpringApplication can create the correct ApplicationContext based on the configuration
- If Spring MVC is provided
AnnotationConfigServletWebServerApplicationContext
- If Spring MVC does not provide WebFlux but does
AnnotationConfigReactiveWebApplicationContext
. -
If you haven’t provide you use AnnotationConfigApplicationContext
You can also use setApplication(…) Set the ApplicationContext entirely yourself.
Access application parameters
If need access to SpringApplication.run(…) The args parameter, can pass into the org. Springframework. Boot. ApplicationArguments bean access. The interface ApplicationArguments provides not only String[] to access native arguments, but also option and non-option arguments for 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"]
}
}Copy the code
Use ApplicationRunner and CommandLineRunner
If you need to run additional code after springApplication.run (), you can implement the ApplicationRunner or CommandLineRunner interface, both of which provide a run() method. After springApplication.run () is called, for example:
import org.springframework.boot.* import org.springframework.stereotype.* @Component public class MyBean implements CommandLineRunner { public void run(String... args) { // Do something... }}Copy the code
If multiple CommandLinerunners or ApplicationRunners are defined, Will be interface to org. Springframework. Core. Ordered or org. Springframework. Core. The annotation. The Order comments provide Order.
Application to exit
Each SpringApplication registers a hook that the JVM exits, ensuring that the ApplicationContext can be gracefully closed. Another bean can realize org. Springfamework. Boot. When calling SpringApplication ExitCodeGenerator interface. The exit () returns a specific return code, this return code can be passed to the System. The exit () such as:
@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)));
}
}Copy the code