Backend Development 6 min read

Master Spring Boot Startup Tasks: Runners, Config, and Command-Line Tricks

After launching a Spring Boot 2.3.9 application, you can execute custom tasks using ApplicationRunner or CommandLineRunner, access startup arguments, generate random configuration values, disable command-line properties, understand configuration file loading order, and correctly use @EnableConfigurationProperties for bean binding.

Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Spring Full-Stack Practical Cases
Master Spring Boot Startup Tasks: Runners, Config, and Command-Line Tricks

Environment: Spring Boot 2.3.9

Execute Tasks After Application Startup

Implement

ApplicationRunner

or

CommandLineRunner

to run code after the application has started.

<code>@SpringBootApplication
public class SpringBootFunctionsApplication implements ApplicationRunner {
    public static void main(String[] args) {
        SpringApplication.run(SpringBootFunctionsApplication.class, args);
        System.out.println("main method invoke...");
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
        System.out.println("ApplicationRunner executed after startup: " + Arrays.toString(args.getSourceArgs()));
    }
}</code>

Execution result:

The

run

method of a Runner is invoked before

SpringApplication.run

completes.

Access Application Startup Arguments in Beans

You can inject

ApplicationArguments

into any bean to read the command‑line parameters.

<code>@Configuration
public class ArgumentConfig {
    @Bean
    public Object object(ApplicationArguments args) {
        System.out.println(Arrays.toString(args.getSourceArgs()));
        return new Object();
    }
}</code>

Execution result:

Generate Random Values in Configuration Files

Use Spring Boot’s

${random.*}

placeholders in

application.yml

or

application.properties

:

<code>user:
  password: ${random.value}
  age: ${random.int(100)}
  id: ${random.long}
  uuid: ${random.uuid}</code>

Bind them to a bean:

<code>@Component
@ConfigurationProperties(prefix = "user")
public class RandomProperties {
    private String password;
    private int age;
    private long id;
    private String uuid;
    // getters and setters omitted
}</code>

Print the values after startup:

<code>@SpringBootApplication
public class SpringBootFunctionsApplication implements ApplicationRunner, ApplicationContextAware {
    private ApplicationContext ctx;
    public static void main(String[] args) {
        SpringApplication.run(SpringBootFunctionsApplication.class, args);
    }
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) {
        this.ctx = applicationContext;
    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
        RandomProperties props = ctx.getBean(RandomProperties.class);
        System.out.println(props.getUuid() + "\n" + props.getPassword() + "\n" + props.getId() + "\n" + props.getAge());
    }
}</code>

Execution result:

Disable Command‑Line Properties

When running a Spring Boot jar, command‑line arguments like

--server.port=5555

are added to the environment by default. To prevent this, disable the feature programmatically:

<code>public static void main(String[] args) {
    SpringApplication app = new SpringApplication(SpringBootFunctionsApplication.class);
    app.setAddCommandLineProperties(false);
    app.run(args);
    System.out.println("main method invoke...");
}</code>

Execution result:

Configuration File Loading Locations and Priority

Spring Boot loads configuration files in the following order (higher priority overrides lower):

/config subdirectory of the current directory

Current directory

/config directory on the classpath

Root of the classpath

By default it reads

application.properties

or

application.yml

. You can specify a different name:

<code>java -jar xxx.jar --spring.config.name=custom</code>

Or provide explicit locations:

<code>java -jar xxx.jar --spring.config.location=classpath:/default.properties,classpath:/override.properties</code>

YAML Configuration File Limitation

Spring’s

@PropertySource

does not support YAML files; it works only with

.properties

files.

<code>@Configuration
@PropertySources({@PropertySource("classpath:config.yml")})
public class ArgumentConfig {
    // This will not be processed because YAML is not supported
}</code>

Using @EnableConfigurationProperties

If a class is annotated only with

@ConfigurationProperties

and not with

@Component

, it will not be registered as a bean. Adding

@EnableConfigurationProperties

to a configuration class registers it:

<code>@Configuration
@EnableConfigurationProperties(RandomProperties.class)
public class ArgumentConfig {
}
@ConfigurationProperties(prefix = "user")
public class RandomProperties {
    private String password;
    private int age;
    private long id;
    private String uuid;
    // getters and setters omitted
}</code>

The above setup makes the

RandomProperties

bean available for injection.

Javabackend developmentSpring BootConfigurationPropertiesApplicationRunner
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.