21

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.

Community
  • 1
  • 1
Muatik
  • 4,011
  • 10
  • 39
  • 72
  • According to docs @ ConfigurationProperties works only in @ Configuration annotated classes – Nadir Jan 18 '17 at 12:32
  • I annotated ConfigBinder with `@Configuration` but it still did not work. – Muatik Jan 18 '17 at 12:34
  • 1
    It's the setter for the property you are missing. I also was wondering why my property values are not being filled. If you use the @Value you can keep things private. If you want the prefix thing to work, add just a setter for the property to be set. – anydoby Oct 18 '17 at 07:50

2 Answers2

17

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

}
Strelok
  • 50,229
  • 9
  • 102
  • 115
  • I've made the changes but the result is same. updated code is here: https://github.com/muatik/spring-playground/tree/master/spring-profiles-example1 – Muatik Jan 18 '17 at 12:48
  • 3
    @Muatik add setters for your fields. They are required, because standard bean property binding is used by `@ConfigurationProperties`. I updated my answer. – Strelok Jan 18 '17 at 13:01
  • 2
    Or use lombok `@Data` to generate all the necessary methods for `ConfigBinder`. – OrangeDog Jan 18 '17 at 13:52
  • This is not correct, you don't need to use "@EnableConfigurationProperties", it's a choice : "Enable support for ConfigurationProperties annotated beans. ConfigurationProperties beans can be registered in the standard way (for example using @Bean methods) or, for convenience, can be specified directly on this annotation." – Tristan Jul 25 '18 at 13:54
  • 1
    The minimum is the `setter` method – Marcello DeSales Dec 03 '18 at 05:44
  • 1
    So did y'all figure it out? Cause I did all of that and still getting the same issue – TheLebDev Sep 22 '22 at 13:40
11

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 !!!
}
Cyva
  • 1,057
  • 10
  • 9