简体   繁体   English

Spring Boot:如何使用ConfigurationProperties获取程序args

[英]Spring Boot : How to get program args with ConfigurationProperties

I'm trying to get some properties from the command line when I start my app. 启动我的应用程序时,我试图从命令行获取一些属性。 I really try a lot of solution but nothings is working. 我确实尝试了很多解决方案,但没有任何效果。 I'll only use @Value as a last solution 我只会使用@Value作为最后的解决方案

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

Here's some classes 这是一些课程

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.....I have tested your code.It is setProperty that missed in StringProperties. 操作.....我已经测试了您的代码。StringProperties中缺少setProperty。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM