Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ConfigurationProperties does not bind properties

Tags:

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.

my application.properties:

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

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

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

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

like image 381
Muatik Avatar asked Jan 18 '17 12:01

Muatik


People also ask

What is the use of @ConfigurationProperties?

@ConfigurationProperties allows to map the entire Properties and Yaml files into an object easily. It also allows to validate properties with JSR-303 bean validation. By default, the annotation reads from the application.

How do you call a properties file in spring boot?

Another method to access values defined in Spring Boot is by autowiring the Environment object and calling the getProperty() method to access the value of a property file.

How do I read properties file in spring boot main class?

Another very simple way to read application properties is to use @Value annotation. Simply annotation the class field with @Value annotation providing the name of the property you want to read from application. properties file and class field variable will be assigned that value.

What is relaxed binding in spring boot?

Relaxed binding maps the Environment property to the bean property name even it is not an exact match. For example, dash-separated environment properties (app-name bound to appName) or capitalized properties as in PORT (bound to port). Spring boot supports relaxed binding.


2 Answers

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

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

}
like image 183
Strelok Avatar answered Sep 28 '22 08:09

Strelok


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

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

    private String name;

    private String url;

    // Getters and Setters !!!
}
like image 26
Cyva Avatar answered Sep 28 '22 07:09

Cyva