gpt4 book ai didi

java - Spring 启动: How to get program args with ConfigurationProperties

转载 作者:行者123 更新时间:2023-12-01 18:00:03 24 4
gpt4 key购买 nike

当我启动我的应用程序时,我试图从命令行获取一些属性。我确实尝试了很多解决方案,但没有任何效果。我只会使用@Value作为最后的解决方案

java -jar myjar.jar --test.property=something

这里有一些类(class)

Main class

@SpringBootApplication
public class TestApplication {

public static void main(String[] args) {
SpringApplicationBuilder builder = new SpringApplicationBuilder(TestApplication.class);
builder.run(args);
}
}

Config class

@Configuration
@EnableConfigurationProperties(StringProperties.class)
public class StringConfiguration {

@Autowired
private StringProperties stringProperties;

@Bean(name = "customBeanName")
public List<String> properties() {
List<String> properties = new ArrayList<>();

properties.add(stringProperties.getString());

return properties;
}
}

Properties class

@Component
@ConfigurationProperties("test")
public class StringProperties {

private String property;

public String getString() {
return property;
}
}

最佳答案

Ops.....我已经测试了你的代码。它是 StringProperties 中缺少的 setProperty。

 public void setProperty(String property) {
this.property = property;
}

关于java - Spring 启动: How to get program args with ConfigurationProperties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41732476/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com