Issue
I found the below code somewhere
@SpringBootApplication
public class UsingCommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(UsingCommandLineRunner.class);
}
@Bean
public CommandLineRunner demo(){
return (args) -> {
// do something
};
}
}
How does this even working?
Who is invoking demo
?
@SpringBootApplication
public class UsingCommandLineRunner {
public static void main(String[] args) {
SpringApplication.run(UsingCommandLineRunner.class);
System.out.println("Args inside main()");
for(int i=0; i<args.length; i++){
System.out.println(args[i]);
}
}
@Bean
public CommandLineRunner demo(){
return (args) -> {
System.out.println("Args inside demo()");
for(int i=0; i<args.length; i++){
System.out.println(args[i]);
}
System.out.println("Using CommandLineRunner");
};
}
}
Upon running the above code, the below is what got printed.
Args inside demo()
Using CommandLineRunner
Args inside main()
userName=Rahul_Kumar
Why command line arguments are not inside args
inside demo
?
Solution
you should run this app use SpringApplication.run(UsingCommandLineRunner.class,args);
Answered By - DingHao
Answer Checked By - Candace Johnson (JavaFixing Volunteer)