2017-01-18 108 views
3

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

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

输出:

... 
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 not retrieving properties from Environment

我发现我错过了ConfigBinder的制定者。添加它们之后,它现在可以工作。

+0

根据文档@ ConfigurationProperties只能在@配置注释类 – Nadir

+0

我注释ConfigBinder与'@ Configuration',但它仍然没有奏效。 – Muatik

+0

这是你失踪的财产的二传手。我也想知道为什么我的房产价值没有被填补。如果您使用@Value,则可以将事物保密。如果你想要前缀的东西工作,只需添加一个setter来设置属性。 – anydoby

回答

3

你需要从你的属性类别中删除@Component并添加setter方法,因为标准的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; 
    } 
} 

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

} 
+0

我做了更改,但结果相同。更新代码在这里:https://github.com/muatik/spring-playground/tree/master/spring-profiles-example1 – Muatik

+1

@Muatik为你的领域添加setter。它们是必需的,因为'@ ConfigurationProperties'使用标准的bean属性绑定。我更新了我的答案。 – Strelok

+0

yeap,我从另一个SO问题中找到了它。谢谢。 – Muatik

1

主要问题是你没有setter。当你将setter设置为ConfigBuilder时可以正常工作。该ConfigBuilder必须是这样

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

    private String name; 

    private String url; 

    // Getters and Setters !!! 
}