LoginSignup
2
1

More than 5 years have passed since last update.

DropwizardのConfiguration

Last updated at Posted at 2013-12-22

Hello Worldから続けてUser Manualを確認していく。

Dropwizardのバージョンは0.6.2。

設定ファイル

使用できる設定ファイルはyamlとjson。
ファイルの拡張子がyml、yamlでない場合はjsonとしてパースをするとのこと。

message-queue.yml
messageQueue:
  host: mq.example.com
  port: 5673
message-queue.json
{
  "messageQueue" : {
    "host" : "mq.example.com",
    "port" : 5678
  }
}

設定のグループ化

Configurationはある程度のまとまりでグループ化することができる。
入れ子になったyamlに対応するConfigurationは以下のとおり。

message-queue.yml
messageQueue:
  host: mq.example.com
  port: 5673
MessageQueueConfiguration.java
package example.core.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;
import org.hibernate.validator.constraints.NotEmpty;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;

public class MessageQueueConfiguration {

    @NotEmpty
    @JsonProperty
    private String host;

    @Min(1)
    @Max(65535)
    @JsonProperty
    private int port = 5672;

    public String getHost() {
        return host;
    }

    public int getPort() {
        return port;
    }
}
ExampleServiceConfiguration.java
package example.core.configuration;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.yammer.dropwizard.config.Configuration;

import javax.validation.Valid;
import javax.validation.constraints.NotNull;

public class ExampleServiceConfiguration extends Configuration {

    @Valid
    @NotNull
    @JsonProperty
    private MessageQueueConfiguration messageQueue = new MessageQueueConfiguration();

    public MessageQueueConfiguration getMessageQueue() {
        return messageQueue;
    }
}

設定の検証

ConfigurationはHibernate Validatorのアノテーションを付与することでサーバ起動時に検証することができる。
入れ子になっている場合も@Validで検証可能。

システムプロパティで上書き

サーバ起動時のコマンドに-Ddw.(設定のパス)=(値)を追加することでyamlファイルの設定値を上書きすることができる。

java -jar example.jar -Ddw.messageQueue.port=5432 server message-queue.yml
2
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
1