Backend Development 8 min read

Master SpringBoot 2.7: Access Params, Banner, Converters, and Graceful Shutdown

This tutorial demonstrates how to retrieve custom application parameters, inject and print the banner, use ConversionService for type conversion, access servlet‑related beans, work with MessageSource for i18n, publish events, and gracefully shut down a SpringBoot 2.7 application.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master SpringBoot 2.7: Access Params, Banner, Converters, and Graceful Shutdown

1. Application Parameters

SpringBoot can receive startup arguments such as:

<code>java -jar app.jar --pack.title=xxx --pack.version=1.0.0</code>

These arguments can be accessed via Environment :

<code>@Resource
private Environment env;
public void getArgs() {
  String title = env.getProperty("pack.title");
  String version = env.getProperty("pack.version");
}</code>

SpringBoot also registers an ApplicationArguments bean that can be injected directly:

<code>@Resource
private ApplicationArguments applicationArguments;
public void getArgs() {
  List&lt;String&gt; titles = applicationArguments.getOptionValues("pack.title");
  List&lt;String&gt; versions = applicationArguments.getOptionValues("pack.version");
}</code>

You can obtain the raw argument array:

<code>String[] args = applicationArguments.getSourceArgs();</code>

Output example:

<code>[--pack.title=xxx, --pack.version=1.0.0]</code>

2. Banner

Inject the Banner bean to print the banner programmatically:

<code>@Resource
private Banner banner;
@Resource
private Environment env;
public void printBanner() {
  banner.printBanner(env, PackApplication.class, System.out);
}</code>

Ensure the banner is not disabled (no spring.main.banner-mode: off configuration) or the injection will fail. It can also be disabled programmatically:

<code>SpringApplication app = new SpringApplication(PackApplication.class);
app.setBannerMode(Mode.OFF);
</code>

3. Type Converters

The ConversionService converts configuration values and request parameters:

<code>@Resource
private ConversionService conversionService;
Integer ret = conversionService.convert("6666",
  TypeDescriptor.valueOf(String.class),
  TypeDescriptor.valueOf(Integer.class));</code>

Custom converters can be registered via WebMvcConfigurer :

<code>@Configuration
public class WebConfig implements WebMvcConfigurer {
  @Override
  public void addFormatters(FormatterRegistry registry) {
    registry.addConverterFactory(new ConverterFactory&lt;String, User&gt;() {
      @Override
      public &lt;T extends User&gt; Converter&lt;String, T&gt; getConverter(Class&lt;T&gt; targetType) {
        return source -> {
          String[] s = source.split(",");
          return (T) new User(Integer.valueOf(s[0]), s[1]);
        };
      }
    });
  }
}
</code>

4. Servlet‑Related Beans

SpringBoot registers the following servlet‑related beans that can be injected safely:

ServletRequest

ServletResponse

HttpSession

WebRequest

These beans are backed by ObjectFactory and retrieve the actual request/response from a ThreadLocal:

<code>@Resource
private HttpServletRequest request;
@Resource
private HttpServletResponse response;
public void getParam() {
  String value = request.getParameter("name");
}</code>

5. Internationalization (MessageSource)

Inject MessageSource to obtain localized messages:

<code>@Resource
private MessageSource messageSource;
public void getMsg() {
  String message = messageSource.getMessage(
    "pack.info.message",
    new Object[]{"张三"},
    "我是默认消息",
    Locale.CHINA);
}</code>

If no default message is provided, Spring uses DelegatingMessageSource , which is empty. Configure the base name in application.yml to enable i18n:

<code>spring:
  messages:
    basename: i18n/message</code>

SpringBoot then registers a ResourceBundleMessageSource . Alternatively, you can inject ConfigurableApplicationContext and call getMessage , which internally uses the same MessageSource :

<code>@Resource
private ConfigurableApplicationContext context;
String message = context.getMessage(
  "pack.info.message",
  new Object[]{"张三"},
  "我是默认消息",
  Locale.CHINA);
</code>

6. Event Publishing

Publish events via the application context:

<code>@Resource
private ConfigurableApplicationContext context;
context.publishEvent(new TxApplicationEvent(context));
</code>

Spring also registers an ApplicationEventMulticaster bean for broadcasting events:

<code>@Resource
private ApplicationEventMulticaster eventMulticast;
eventMulticast.multicastEvent(new TxApplicationEvent(context));
</code>

You can provide a custom bean named applicationEventMulticaster to replace the default implementation.

7. Graceful Shutdown

When running an embedded Tomcat jar, SpringBoot registers WebServerGracefulShutdownLifecycle to allow graceful shutdown:

<code>@Resource
private WebServerGracefulShutdownLifecycle webServerGracefulShutdown;
public void shutdownWebServer() {
  webServerGracefulShutdown.stop(() -> {
    System.out.println("Gracefully shutting down Web Server");
    context.close();
  });
}
</code>

Enable graceful shutdown in configuration:

<code>server:
  shutdown: graceful</code>

After invoking the shutdown, the console prints the message shown below.

JavaSpringBootGracefulShutdownConversionServiceBannerApplicationArgumentsMessageSource
Spring Full-Stack Practical Cases
Written by

Spring Full-Stack Practical Cases

Full-stack Java development with Vue 2/3 front-end suite; hands-on examples and source code analysis for Spring, Spring Boot 2/3, and Spring Cloud.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.