2017-01-18 12 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は唯一私が 'Configuration' @それはまだ動作しませんでしたとConfigBinderを注釈付き@設定注釈付きクラス – Nadir

+0

で動作します。 – Muatik

+0

不足しているプロパティの設定者です。私の不動産価値が満たされていない理由も不思議に思っていました。 @Valueを使用すると、物事を非公開にすることができます。接頭辞を機能させたい場合は、設定するプロパティのセッターだけを追加してください。 – anydoby

答えて

3

あなたはプロパティクラスから@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; 
    } 
} 

そして、あなたのメインクラスに@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あなたのフィールドのセッターを追加してください。デフォルトのBeanプロパティバインディングは '@ ConfigurationProperties'によって使用されるため、これらは必須です。私は自分の答えを更新しました。 – Strelok

+0

yeap、別のSOの質問からこれを見つけました。ありがとう。 – Muatik

1

主な問題は、あなたがセッターを持っていないということです。設定ツールをConfigBuilderに置くとうまく動作します。 ConfigBuilderは、ドキュメントによると、この

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

    private String name; 

    private String url; 

    // Getters and Setters !!! 
} 
関連する問題