ConfigurationProperties

ConfigurationProperties

本文介绍了ConfigurationProperties不绑定属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我的application.properties:

my application.properties:

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

application.java

application.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

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

输出:

...
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

此实现有什么问题?

修改和解决方案:可能的重复: Spring Boot @ConfigurationProperties无法从环境中检索属性

我发现我错过了ConfigBinder中的设置器.添加它们后,它现在可以使用.

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

推荐答案

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

You need to remove @Component from you properties class and add setters because standard bean property binding is used by @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;
    }
}

并在您的主类中添加@EnableConfigurationProperties:

And add @EnableConfigurationProperties to your main class:

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

}

这篇关于ConfigurationProperties不绑定属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-26 22:15