简体   繁体   English

在服务中读取application.yml

[英]Reading application.yml in service

I have a oracle rest end point, and I read the entries from there. 我有一个oracle休息终点,并且从那里读取条目。 This is my application.yml 这是我的application.yml

spring:
  profiles:
    active: dev
oracleUrl: https://cloud-url/filter_by/login_id/

OracleProperties Oracle属性

@Configuration
@ConfigurationProperties
public class OracleProperties {

    @Value("${oracleUrl}")
    String oracleUrl;

    public String getOracleUrl() {
        return oracleUrl;
    }

    public void setOracleUrl(String oracleUrl) {
        this.oracleUrl = oracleUrl;
    }
}

Application.java Application.java

@EnableConfigurationProperties({OracleProperties.class})
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

Service 服务

@Autowired
private OracleProperties oracleProperties;

String oracleUrl = oracleProperties.getOracleUrl();

My oracleUrl is null, Please let me know where I am going wrong. 我的oracleUrl为null,请让我知道我要去哪里了。

You don't need @Configuration annotation on OracleProperties . 您不需要在OraclePropertiesOracleProperties @Configuration批注。 The @ConfigurationProperties("oracle") will pick up any properties with prefix oracle . @ConfigurationProperties("oracle")将选择所有前缀为oracle属性。 Helps to group a family of properties with oracle prefix. 有助于使用oracle前缀对一组属性进行分组。 Also Notice, there is no @Value on top of url attribute. 另请注意, url属性顶部没有@Value

@ConfigurationProperties("oracle")
public class OracleProperties {

    private String url;

    public String getUrl() {
        return oracleUrl;
    }

    public void setUrl(String url) {
        this.url = url;
    }
}

Changes to your application.yml , application.yml更改,

oracle:
  url: https://cloud-url/filter_by/login_id/

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

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