LoginSignup
34
44

More than 5 years have passed since last update.

Spring Bootでプロパティファイルとのバインディング方法

Last updated at Posted at 2018-03-09

Spring Bootにおいて、プロパティファイルとのバインディング方法をまとめました。

単一項目のバンティング

以下のプロパティをJavaフィールドとバインディングする。

application.properties
foo.intervel=1000
bar.intervel=500

まずは、プレイフィックス(foo, bar)毎に@ConfigurationPropertiesでプレイフィックス指定でコンテキストクラスを作成する。

@ConfigurationProperties(prefix="foo")
public class FooContext {

    private int intervel;

    public int getIntervel() {
        return intervel;
    }

    public void setIntervel(int intervel) {
        this.intervel = intervel;
    }
}
@ConfigurationProperties(prefix="bar")
public class BarContext {

    private int intervel = 10; // ここでデフォルト値を指定可能

    public int getIntervel() {
        return intervel;
    }

    public void setIntervel(int intervel) {
        this.intervel = intervel;
    }
}

後は、コンテキストクラスをDIに登録すればOK。

@Configuration
public class PropertiesConfiguration {

    @Bean
    public FooContext fooConfig() {
        return new FooContext();
    }

    @Bean
    public BarContext barConfig() {
        return new BarContext();
    }
}

以下の感じてプロパティ値を取得する。

    @Autowired
    private FooContext fooContext;

    @Autowired
    private BarContext barContext;

    public void test {
        int fooIntervel = fooContext.getIntervel();
        int barIntervel = barContext.getIntervel();
    }

キャメルケースの場合、ハイフンで単語を連結するか、キャメルのままでもOK。

@ConfigurationProperties(prefix="foo")
public class FooContext {

    private int itemName;

    // getter & setter
}
application.properties
foo.item-name=apple
application.properties
foo.itemName=apple

Collection型とのバインディング

リストや配列の場合、以下の感じてプロパティとバインディングする。

@ConfigurationProperties(prefix="foo")
public class FooContext {

    private List<String> items;    //or private String[] items;

    // getter & setter
}
application.properties
foo.items[0]=apple
foo.items[1]=banana

カンマ区切りの書き方もOKです。

application.properties
foo.items=apple,banana,...

Map型とのバインディング

Map型の場合、以下の感じてプロパティとバインディングする。

@ConfigurationProperties(prefix="foo")
public class FooContext {

    private Map<String, String> itemMap;

    // getter & setter
}
application.properties
foo.item-map.key1=apple
foo.item-map.key2=banana

ネストオブジェクトのバインディング

ネストオブジェクトの場合、以下の感じてプロパティとバインディングする。

@ConfigurationProperties(prefix="foo")
public class FooContext {

    private Product product;    

    // getter & setter
}
public class Product {

    private String productId;
    private String productName;

    // getter & setter
application.properties
foo.product.product-id=001
foo.product.product-name=apple

リスト+ネストの場合も同様です。

@ConfigurationProperties(prefix="foo")
public class FooContext {

    private List<Product> products; 

    // getter & setter
}
application.properties
foo.products[0].product-id=001
foo.products[0].product-name=apple
foo.products[1].product-id=002
foo.products[1].product-name=banana

classpath内複数プロパティファイルとのバインディング

クラスパス内複数のプロパティファイルを指定する場合、@PropertySourcesを利用する。

@SpringBootApplication
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource("classpath:another.properties")
})
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

外部プロパティのバインディング

外部プロパティファイルを利用する場合、起動のコマンドラインにファイルパスを指定する。

java -jar demo.jar --spring.config.location="file:/tmp/properties/application.properties"
34
44
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
34
44