This section dives into the details of Spring Boot.
Here you can learn about the key features that you may want to use and customize.
If you have not already done so, you might want to read the
and
"Part III, “Using Spring Boot”" sections,
so that you have a good grounding of the basics.
23. SpringApplication
The SpringApplication class provides a convenient way to bootstrap a Spring application that is started from a main() method.
In many situations, you can delegate to the static SpringApplication.run method, as shown in the following example:
public static void main(String[] args) {
SpringApplication.run(MySpringConfiguration.class, args);
}
When your application starts, you should see something similar to the following output:
23.1 Startup Failure
If your application fails to start, registered FailureAnalyzers get a chance to provide a dedicated error message and a concrete action to fix the problem.
For instance, if you start a web application on port 8080 and that port is already in use, you should see something similar to the following message:
23.2 Customizing the Banner
The banner that is printed on start up can be changed by adding a banner.txt file to your classpath or by setting the spring.banner.location property to the location of such a file.
If the file has an encoding other than UTF-8, you can set spring.banner.charset. In addition to a text file, you can also add a banner.gif, banner.jpg, or banner.png image file to your classpath or set the spring.banner.image.location property. Images are converted into an ASCII art representation and printed above any text banner.
23.3 Customizing SpringApplication
If the SpringApplication defaults are not to your taste, you can instead create a local instance and customize it. For example, to turn off the banner, you could write:
public static void main(String[] args) {
SpringApplication app = new SpringApplication(MySpringConfiguration.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
23.5 Application Events and Listeners
In addition to the usual Spring Framework events, such as ContextRefreshedEvent
, a SpringApplication
sends some additional application events.
Application events are sent in the following order, as your application runs:
1.An ApplicationStartingEvent is sent at the start of a run but before any processing, except for the registration of listeners and initializers.
2.An ApplicationEnvironmentPreparedEvent is sent when the Environment to be used in the context is known but before the context is created.
3.An ApplicationPreparedEvent is sent just before the refresh is started but after bean definitions have been loaded.
4.An ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
5.An ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests.
6.An ApplicationFailedEvent is sent if there is an exception on startup.