Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Spring启动-来自yml的配置值为空_Java_Spring_Spring Boot_Yaml - Fatal编程技术网

Java Spring启动-来自yml的配置值为空

Java Spring启动-来自yml的配置值为空,java,spring,spring-boot,yaml,Java,Spring,Spring Boot,Yaml,im使用spring配置注释加载配置yml。使用我配置的4个值中的3个,一切正常。但是,第四个值为空 由于其他值加载正确,我认为没有配置错误。我不知道 这是我的密码: Yml文件 spring: profiles: test airtable: api-key: xxx base: xxx proxy: "localhost:8095" url: testUrl mail: subjectPrefix: R750Explorer develop

im使用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;
}


}
自动连线到这里

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键、基和代理中定义的值。然而,url是空的

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

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

上述yml是应用测试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下的default application.yml中添加
url:testurl
,它会写入值。但是,它没有在application-test.yml中发送。虽然这仅适用于url,而不适用于代理等,但它们工作正常。

它适用于我,没有任何更改:

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

伙计们,我找到了答案。正如我在问题中已经说过的,我使用一个default application.yml和一个application-test.yml

这两个文件位于不同的资源文件夹中(src/resource文件夹中有一份test.yml的副本,其中有语法错误)

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

| src/资源/

|-->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=****
| 测试/资源/

|-->application-test.yml


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

你能试试url:'testUrl'吗?试试url:'testUrl'。没有更改。您是否为
AirtableProperties
类中的
url
字段定义了
getter
setter
方法?是,添加了它们,所以问题很清楚。你能在调试模式下运行你的应用程序,在
setUrl
方法中放置一个断点并检查其中的值吗?在你的示例中,你将代理设置为
localhost:8095
,而在最初的帖子中,它被设置为
“localhost:8095”
,所以这是不同的,@PaulNUK在阅读了某人的评论后编辑了这个问题。看到时间的回答vs问题编辑好的,我怀疑引用是混乱的,基于事实,你的版本工作良好。