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.
Environment: Spring Boot 2.3.9
Execute Tasks After Application Startup
Implement
ApplicationRunneror
CommandLineRunnerto 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
runmethod of a Runner is invoked before
SpringApplication.runcompletes.
Access Application Startup Arguments in Beans
You can inject
ApplicationArgumentsinto 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.ymlor
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=5555are 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.propertiesor
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
@PropertySourcedoes not support YAML files; it works only with
.propertiesfiles.
<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
@ConfigurationPropertiesand not with
@Component, it will not be registered as a bean. Adding
@EnableConfigurationPropertiesto 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
RandomPropertiesbean available for injection.
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.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.