我有一个春季启动应用程序。我想在执行jar时覆盖在application.yml中配置的某些属性。
我的代码是这样的:
@Service
public class CommandService {
@Value("${name:defaultName}")
private String name;
public void print() {
System.out.println(name);
}
}
Run Code Online (Sandbox Code Playgroud)
而且Application.java是
@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();
}
}
Run Code Online (Sandbox Code Playgroud)
application.yml
名称:nameInApplication
当我执行这样的命令时:
java -jar app.jar --name = nameInCommand
不行
第二个命令也不起作用:
java -Dname = nameInCommand2 -jar app.jar
但是,如果application.yml没有配置名称,则第二个命令将正常运行,而第一个命令也将不起作用。