Part IV. Spring Boot features

This section dives into the details of Spring Boot.

image.png

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

"Part II, “Getting Started”"

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.


image.png

In many situations, you can delegate to the static SpringApplication.run method, as shown in the following example:


image.png
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.

image.png

image.png

image.png

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.

image.png

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.

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容