2017-12-29 27 views
0

私はこのようなymlファイルを読み込もうとしています。@Valueは読み取ることができますが、@ConfigurationPropertiesはできません

order: 
    foo: 5000 
    bar: 12 

と私は@valueと読むことができます。 YMLファイルは、より複合化であることを行っているので、私は@ConfigurationPropertiesを使用しようとしている

@Component 
@Data 
public class WebConfigProperty { 

    private Integer foo; 
    private Integer bar; 

    public WebConfigProperty(@Value("${order.foo}") @NonNull final Integer foo, 
      @Value("${order.bar}") @NonNull final Integer bar) { 
     super(); 
     this.foo = foo; 
     this.bar = bar; 
    } 
} 

(私はところでロンボクを使用しています)。しかし、@ConfigurationPropertiesでは動作しません。

@Component 
@ConfigurationProperties("order") 
@Data 
public class WebConfigProperty { 

    @NonNull 
    private Integer foo; 
    @NonNull 
    private Integer bar; 
} 

また、コンフィグクラスで@EnableConfigurationPropertiesを追加しました。設定内のすべての注釈は次のとおりです。

@SpringBootConfiguration 
@EnableConfigurationProperties 
@EnableAutoConfiguration(exclude = { ... }) 
@ComponentScan(basePackages = { ... }) 
@Import({ ... }) 
@EnableCaching 

エラーメッセージは次のとおりです。

*************************** 
APPLICATION FAILED TO START 
*************************** 

Description: 

Parameter 0 of constructor in {...}.WebConfigProperty required a bean of type 'java.lang.Integer' that could not be found. 


Action: 

Consider defining a bean of type 'java.lang.Integer' in your configuration. 

春はYMLファイルを検索し、WebConfigPropertyフィールドにNULL値を入れしようとしていることができないように思えます。どうしてか分かりません。

FYI、これはGradleを使用したマルチプロジェクトアプリケーションです。 ymlファイルと構成クラス(書き込まれていない)は同じプロジェクトにあります。 WebConfigPropertyは別のプロジェクトにあります。

編集: @Yannic Klemの回答に基づいて、この2つが機能しました。

@Component 
@ConfigurationProperties("order") 
@Getter 
@Setter 
@EqualsAndHashCode 
public class WebConfigProperty { 

    @NonNull 
    private Integer foo; 
    @NonNull 
    private Integer bar; 
} 

//OR 

@Component 
@ConfigurationProperties("order") 
@Data 
@NoArgsConstructor 
public class WebConfigProperty { 

    @NonNull 
    private Integer foo; 
    @NonNull 
    private Integer bar; 
} 
+0

私はこの問題は、 'Data' @注釈だと思います。私は答えを出しましたが、現時点では確認できません。 '@Data'アノテーションでない場合は教えてください –

+2

' @Data'の代わりに '@ Getter'と' @Setter'を明示的に使ってみてください – pvpkiran

+0

'@ConfigurationProperties(prefix =" order ")'で試してみませんか? –

答えて

2

Lomboks @Data注釈が@RequiredArgsConstructor追加されます。 次に、Springはコンストラクタへの引数をオートワイヤ化しようとします。

Integerの2つのbean:fooとbarを検索しようとするため、例外が発生します。

@ConfigurationPropertiesには、デフォルトのコンストラクタとそのプロパティのgetters + setterのみが必要です。 プロパティは、これらのセッターによってクラス@ConfigurationPropertiesにバインドされます。

あなたWebConfigPropertyは、次のようになります。

@Component 
@ConfigurationProperties("order") 
/** 
* Not sure about IDE support for autocompletion in application.properties but your 
* code should work. Maybe just type those getters and setters yourself ;) 
*/ 
@Getters 
@Setters 
public class WebConfigProperty { 

    @NonNull 
    private Integer foo; 
    @NonNull 
    private Integer bar; 
} 
+0

それは働いた。ありがとうございました。 – user2652379

関連する問題