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.

@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()));
    }
}

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.

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

Execution result:

Generate Random Values in Configuration Files

Use Spring Boot’s ${random.*} placeholders in application.yml or application.properties:

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

Bind them to a bean:

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

Print the values after startup:

@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() + "
" + props.getPassword() + "
" + props.getId() + "
" + props.getAge());
    }
}

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:

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

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: java -jar xxx.jar --spring.config.name=custom Or provide explicit locations:

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

YAML Configuration File Limitation

Spring’s @PropertySource does not support YAML files; it works only with .properties files.

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

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:

@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
}

The above setup makes the RandomProperties bean available for injection.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Backend 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

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.