Backend Development 9 min read

Master Spring Boot 3.0.9: Access Command‑Line, JSON, YAML, Encryption & More

This guide demonstrates how to retrieve command‑line arguments, use JSON‑encoded properties, load external configuration files, encrypt values, parse YAML, generate random properties, handle internationalization, customize the banner, and configure the application context in Spring Boot 3.0.9, providing code examples for each feature.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot 3.0.9: Access Command‑Line, JSON, YAML, Encryption & More

1. Access command‑line properties

By default SpringApplication converts any command‑line option that starts with -- into a property and adds it to the Spring Environment . Command‑line properties have higher precedence than file‑based sources. To disable this, call SpringApplication.setAddCommandLineProperties(false) . Example:

<code>public class SpringbootComprehensiveApplication implements CommandLineRunner {
  @Value("${pack.name}")
  private String name;
  @Value("${pack.age}")
  private Integer age;
  public static void main(String[] args) {
    SpringApplication.run(SpringbootComprehensiveApplication.class, args);
  }
  @Override
  public void run(String... args) throws Exception {
    System.out.println(name);
    System.out.println(age);
  }
}
</code>

Output:

<code>张三
66
</code>

You can also inject ApplicationArguments :

<code>@Resource
private ApplicationArguments arguments;
// Access
System.out.println(arguments.getOptionValues("pack.name"));
</code>

2. JSON application properties

Some property names are restricted, so Spring Boot allows encoding properties as a single JSON structure. Any spring.application.json or spring_application_json property is parsed at startup and added to the Environment .

As a system property:

<code>-Dspring.application.json={"user":{"name":"pack"}}</code>

As a command‑line argument:

<code>--spring.application.json={"user":{"name":"pack-args"}}</code>

3. External application properties

Spring Boot automatically loads application.properties and application.yaml from the following locations (ordered by precedence):

Classpath root and /config package.

Current directory and its /config subdirectory, including direct sub‑directories of config .

You can change the configuration file name and location:

<code>java -jar app.jar --spring.config.name=pack</code>
<code>java -jar app.jar --spring.config.location=optional:classpath:/pack.properties,optional:classpath:/other.properties</code>

Using the optional: prefix makes the location optional.

4. Encrypted properties

Spring Boot does not provide built‑in encryption support, but you can manipulate environment values via the EnvironmentPostProcessor interface before the application starts.

5. Direct YAML loading

Spring Framework offers YamlPropertiesFactoryBean to load YAML as Properties and YamlMapFactoryBean to load it as a Map . To load YAML as a PropertySource , use YamlPropertyResourceLoader :

<code>YamlPropertiesFactoryBean factory = new YamlPropertiesFactoryBean();
factory.setResources(new ClassPathResource("com/pack/yml/loader/pack.yml"));
System.out.println(factory.getObject());

YamlMapFactoryBean factory = new YamlMapFactoryBean();
factory.setResources(new ClassPathResource("com/pack/yml/loader/pack.yml"));
</code>

6. Random values

The RandomValuePropertySource can inject random values such as integers, longs, UUIDs, or strings. Example YAML:

<code>pack:
  secret: "${random.value}"
  sno: "${random.int}"
  longnum: "${random.long}"
  uuid: "${random.uuid}"
  number-less-than-ten: "${random.int(10)}"
  number-in-range: "${random.int[1024,65536]}"
</code>

7. Internationalization

Spring Boot supports message localization. By default it looks for message resources at the classpath root. You can configure the base name and other properties under spring.messages :

<code>spring:
  messages:
    basename: message
    fallback-to-system-locale: false
</code>

8. Banner customization

The Banner bean is registered at startup. You can inject it and print a custom banner:

<code>@Resource
private Banner springBootBanner;

public void run(String... args) throws Exception {
    springBootBanner.printBanner(null, null, System.out);
}
</code>

Placeholders such as ${application.version} , ${spring-boot.version} , or ANSI codes can be used in banner.txt .

9. Spring application context (Web environment)

SpringApplication determines the appropriate ApplicationContext type:

If Spring MVC is present, it uses AnnotationConfigServletWebServerApplicationContext .

If Spring MVC is absent but Spring WebFlux is present, it uses a reactive context.

Otherwise, it falls back to AnnotationConfigApplicationContext .

You can override the type with setWebApplicationType(...) or provide a custom ApplicationContextFactory :

<code>new SpringApplication(SpringbootComprehensiveApplication.class)
    .setWebApplicationType(WebApplicationType.SERVLET);
</code>

Finished!

JavaconfigurationSpring Bootyamlproperties
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.