Java ConfigurationProperties 不绑定属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41719466/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 23:36:53  来源:igfitidea点击:

ConfigurationProperties does not bind properties

javaspringspring-boot

提问by Muatik

I want to bind my application.properties into a class automatically by using @ConfigurationProperties annotation. First, I tried with @Value annotation and was able to inject property values into class variables. However, @ConfigurationProperties did not inject properties into values.

我想通过使用 @ConfigurationProperties 注释将我的 application.properties 自动绑定到一个类中。首先,我尝试使用 @Value 注释并且能够将属性值注入到类变量中。但是,@ConfigurationProperties 并没有将属性注入到值中。

my application.properties:

我的 application.properties:

spring.jpa.show-sql=false
my.url=my_url
my.name=muatik

application.java

应用程序.java

package com.muatik;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ApplicationContext;


@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        final ApplicationContext ctx = SpringApplication.run(Application.class, args);
        final ConfigBinder confs = ctx.getBean(ConfigBinder.class);
        System.out.println(confs.getUrl());
        System.out.println(confs.getName());
    }

}

ConfigBinder.java

配置绑定器

package com.muatik;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;



@Component
@ConfigurationProperties(prefix="my")
public class ConfigBinder {

    @Value("${my.name}")
    private String name;

    private String url; // expected to be filled automatically

    public String getUrl() {
        return this.url;
    }

    public String getName() {
        return this.name;
    }
}

output:

输出:

...
2017-01-18 15:19:29.720  INFO 4153 --- [           main] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat started on port(s): 8080 (http)
2017-01-18 15:19:29.724  INFO 4153 --- [           main] com.muatik.Application                   : Started Application in 4.212 seconds (JVM running for 4.937)
null
muatik

What is the wrong with this implementation?

这个实现有什么问题?

edit and solution: possible duplication: Spring Boot @ConfigurationProperties not retrieving properties from Environment

编辑和解决方案:可能的重复:Spring Boot @ConfigurationProperties 不从 Environment 检索属性

I found that I missed the setters in ConfigBinder. After adding them, it works now.

我发现我错过了 ConfigBinder 中的 setter。添加它们后,它现在可以工作了。

采纳答案by Strelok

You need to remove @Component from you properties class and add setters because standard bean property binding is used by @ConfigurationProperties:

您需要从属性类中删除 @Component 并添加设置器,因为以下使用标准 bean 属性绑定@ConfigurationProperties

@ConfigurationProperties(prefix="my")
public class ConfigBinder {

    private String name;

    private String url; // expected to be filled automatically

    public String getUrl() {
        return this.url;
    }

    public String getName() {
        return this.name;
    }

    public void setName(String name) {
        this.name = name;
    }

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

And add @EnableConfigurationProperties to your main class:

并将 @EnableConfigurationProperties 添加到您的主类:

@SpringBootApplication
@EnableConfigurationProperties(ConfigBinder.class)
public class Application {

    public static void main(String[] args) {
        final ApplicationContext ctx = SpringApplication.run(Application.class, args);
        final ConfigBinder confs = ctx.getBean(ConfigBinder.class);
        System.out.println(confs.getUrl());
        System.out.println(confs.getName());
    }

}

回答by Cyva

The main problem is that you do not have setters. When you put setters to ConfigBuilder works fine. The ConfigBuilder must be like this

主要问题是你没有二传手。当您将 setter 放入 ConfigBuilder 时,效果很好。ConfigBuilder 必须是这样的

@Component
@ConfigurationProperties(prefix="my")
public class ConfigBinder {

    private String name;

    private String url;

    // Getters and Setters !!!
}