I have a spring-boot application. I want to override some properties which was configuration in application.yml when executing the jar.
My code like this:
@Service
public class CommandService {
@Value("${name:defaultName}")
private String name;
public void print() {
System.out.println(name);
}
}
And the Application.java is
@SpringBootApplication
public class Application implements CommandLineRunner {
@Autowired
private CommandService commandService;
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Override
public void run(String... args) throws Exception {
commandService.print();
}
}
The application.yml
name: nameInApplication
when I excute the command like that:
java -jar app.jar --name=nameInCommand
It's not work
The Second command also not work:
java -Dname=nameInCommand2 -jar app.jar
But if the application.yml not have config the name, the second command will work well and the first command also not work any way.