相关疑难解决方法(0)

使用命令行参数覆盖spring-boot中的yml配置不起作用

我有一个春季启动应用程序。我想在执行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没有配置名称,则第二个命令将正常运行,而第一个命令也将不起作用。

java configuration command overriding spring-boot

3
推荐指数
1
解决办法
1241
查看次数

标签 统计

command ×1

configuration ×1

java ×1

overriding ×1

spring-boot ×1