繁体   English   中英

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

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

即时通讯使用spring配置注释加载配置yml。 使用我配置的4个值中的3个,一切正常。 但是,第四个值是null。

由于其他值正在正确加载,我不认为存在配置错误。 我无能为力...

这是我的代码:

阳明海运 - 文件

spring:
    profiles: test

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

mail:
    subjectPrefix: R750Explorer develop - 

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;
}


}

在这里自动连线

public class AirtableRepository {

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

private Base base = null;

@Autowired
private AirtableProperties prop;

主要申请文件

@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);
}
}

所以我得到了我在api-key,base和proxy中定义的值。 但是url为null。

=========================编辑======================== =====

更新。 我同时使用默认的application.yml和特定于配置文件的application-test.yml

上面的yml是应用测试的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=****

现在是这里的线索:如果我在airtable下的默认application.yml中添加url: testurl ,它将写入该值。 但是它确实在application-test.yml中。 尽管只有网址而不是代理等情况,但它们工作正常。

它对我有效,没有任何变化:

application.yml:

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

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 + '\'' +
                '}';
    }

输出

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

所以,我找到了答案。 正如我在问题中已经说过的,即时通讯使用默认的application.yml和application-test.yml。

这2个位于单独的资源文件夹中(src / resource文件夹中有一个语法错误的test.yml副本)。

删除Copy并实现此结构后,一切正常:

| SRC /资源/

|-> application.yml

| 测试/资源/

|-> application-test.yml

由于语法错误和文件的错误放置,它没有解决。 感谢所有帮手!

暂无
暂无

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

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