简体   繁体   English

Spring Boot-来自yml的配置值为null

[英]Spring Boot - Configuration Value from yml is null

im loading a configuration yml using spring configuration annotations. 即时通讯使用spring配置注释加载配置yml。 Everything is working fine with 3 of the 4 values I configured. 使用我配置的4个值中的3个,一切正常。 However the 4th Value is null. 但是,第四个值是null。

Since the other Values are loading properly i dont think there is a configuration Error. 由于其他值正在正确加载,我不认为存在配置错误。 Im clueless... 我无能为力...

Here is my Code: 这是我的代码:

Yml-File 阳明海运 - 文件

spring:
    profiles: test

airtable:
    api-key: xxx
    base: xxx
    proxy: "localhost:8095"
    url: testUrl

mail:
    subjectPrefix: R750Explorer develop - 

PropertiesClass PropertiesClass

@Configuration
@ConfigurationProperties(prefix = "airtable")
public class AirtableProperties {

@NotNull
private String apiKey;

@NotNull
private String base;

private String proxy;

private String url;


public String getApiKey() {
    return apiKey;
}

public void setApiKey(String apiKey) {
    this.apiKey = apiKey;
}

public String getBase() {
    return base;
}

public void setBase(String base) {
    this.base = base;
}

public String getProxy() {
    return proxy;
}

public void setProxy(String proxy) {
    this.proxy = proxy;
}

public String getUrl() {
    return url;
}

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


}

Autowired them here 在这里自动连线

public class AirtableRepository {

private final org.slf4j.Logger log = 
LoggerFactory.getLogger(this.getClass());

private Base base = null;

@Autowired
private AirtableProperties prop;

Main Application File 主要申请文件

@SpringBootApplication
@EnableCaching
@EnableScheduling
@EnableConfigurationProperties
public class Application extends SpringBootServletInitializer {


public static void main(String[] args) throws Exception {
.
.
.

public static void main(String[] args) throws Exception {

    SpringApplication.run(Application.class, args);
}
}

So I get the Values I define in api-key,base and proxy. 所以我得到了我在api-key,base和proxy中定义的值。 However url is null. 但是url为null。

========================= Edit ============================= =========================编辑======================== =====

Update. 更新。 I use both a default application.yml and a profile specific application-test.yml 我同时使用默认的application.yml和特定于配置文件的application-test.yml

The above yml is the application-test yml. 上面的yml是应用测试的yml。 Heres the application.yml 继承人application.yml

spring:
    application:
        name: R750Explorer

    boot:
        admin:
            #url: http://localhost:8085       

    devtools:
        restart:
            additional-paths: src, target
            exclude: "**/*.log"

    mail:
        properties:
            mail:
                smp:
                   connectiontimeout: 5000
                   timeout: 3000
                   writetimeout: 5000

    mvc:
        view:
            prefix: /WEB-INF/jsp/
            suffix: .jsp

    output:
        ansi:
            enabled: ALWAYS

    profiles:
        #default: default
        #active: dev 

    airtable:
        api-key: none-default 

    mail:
        from-address: XXX
        to-address: XXX
        user: XXX
        password: XXX

server:
    address: 127.0.0.1
    #port: 9000
    compression:
        enabled: true
    session:
        cookie:
            #comment: # Comment for the session cookie.
            # domain: # Domain for the session cookie.
            http-only: true
            # -> ein Jahr / Maximum age of the session cookie in seconds.
            max-age: 31536000
            #name:  Session cookie name.
            #path: # Path of the session cookie.
            # "Secure" flag for the session cookie.
            secure: true    

logging:
    file: logs/r750explorer.log
    level:
        com:
            sybit: DEBUG

management:
    context-path: /manage
    security: 
        enabled: false
        # roles: SUPERUSER

security:
    user:
        #name: admin
        #password=****

Now heres the clue: If i add url: testurl in the default application.yml under airtable it writes the value. 现在是这里的线索:如果我在airtable下的默认application.yml中添加url: testurl ,它将写入该值。 however it doesent in the application-test.yml. 但是它确实在application-test.yml中。 Although this is only the case for url not for proxy etc, they are working fine. 尽管只有网址而不是代理等情况,但它们工作正常。

It works for me without any changes : 它对我有效,没有任何变化:

application.yml : application.yml:

airtable:
    api-key: xxx
    base: xxx
    proxy: localhost:8095
    url: testUrl

POJO : POJO:

@Configuration
@ConfigurationProperties(prefix = "airtable")
public class AirtableProperties {

    @NotNull
    private String apiKey;

    @NotNull
    private String base;

    private String proxy;

    private String url;

    public String getApiKey() {
        return apiKey;
    }

    public void setApiKey(String apiKey) {
        this.apiKey = apiKey;
    }

    public String getBase() {
        return base;
    }

    public void setBase(String base) {
        this.base = base;
    }

    public String getProxy() {
        return proxy;
    }

    public void setProxy(String proxy) {
        this.proxy = proxy;
    }

    public String getUrl() {
        return url;
    }

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

    @Override
    public String toString() {
        return "AirtableProperties{" +
                "apiKey='" + apiKey + '\'' +
                ", base='" + base + '\'' +
                ", proxy='" + proxy + '\'' +
                ", url='" + url + '\'' +
                '}';
    }

output : 输出

   PROPS ARE AirtableProperties{apiKey='xxx', base='xxx', proxy='localhost:8095', url='testUrl'}

So guys i found the answer. 所以,我找到了答案。 As I already told in the question im using a default application.yml and a application-test.yml. 正如我在问题中已经说过的,即时通讯使用默认的application.yml和application-test.yml。

These 2 were in seperate resource folders ( A copy of the test.yml with a syntax-Error was in the src/resource folder ). 这2个位于单独的资源文件夹中(src / resource文件夹中有一个语法错误的test.yml副本)。

After removing the Copy and implementing this structure everything worked: 删除Copy并实现此结构后,一切正常:

| | src/resources/ SRC /资源/

|--> application.yml |-> application.yml

| | test/resources/ 测试/资源/

|--> application-test.yml |-> application-test.yml

Because of the Syntax-Error and the false placement of the Files it didnt worked out. 由于语法错误和文件的错误放置,它没有解决。 Thanks for all the Helpers! 感谢所有帮手!

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

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