java - Spring Boot - 来自 yml 的配置值为空

标签 java spring spring-boot yaml

我正在使用 spring 配置注释加载配置 yml。使用我配置的 4 个值中的 3 个,一切正常。然而,第四个值是空的。

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

这是我的代码:

Yml 文件

spring:
    profiles: test

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

mail:
    subjectPrefix: R750Explorer develop - 

属性类

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


}

在这里 Autowiring 它们

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 为空。

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

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

上面的yml是application-test 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 中。虽然这只是 url 的情况而不是代理等,但它们工作正常。

最佳答案

伙计们,我找到了答案。正如我在问题中所说,我使用默认的 application.yml 和 application-test.yml。

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

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

| 源/资源/

|--> 应用程序.yml

| 测试/资源/

|--> application-test.yml

由于语法错误和文件的错误放置,它没有成功。感谢所有的 helper !

关于java - Spring Boot - 来自 yml 的配置值为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46149488/

相关文章:

java - map api v2 android : Binary XML file line #2 error

java - 使用Java图形在其中绘制带线的圆

java - spring thymeleaf 将属性传递给选项卡

java - Spring Boot OAuth - 同一用户的多个访问 token

java - 如何将一个对象的特定字段映射到另一个对象?

java - 在 Eclipse 中使用可运行的 Jar 文件时出现 FileNotFoundException 错误

java - Java 日历中的二月

java - Jackson JSON 不包装嵌套对象的属性

java - Spring bean 的 DESTROY-METHOD 属性和 web-application "prototype"d bean

java - 从 Java REST 的 Swagger API 生成单元测试代码