この内容は古いバージョンです。最新バージョンを表示するには、戻るボタンを押してください。
バージョン:2
ページ更新者:atom
更新日時:2021-01-12 11:34:03

タイトル: プロパティファイルの値やjar実行時の引数を取得する方法
SEOタイトル: 【Spring】プロパティファイルの値やjar実行時の引数を取得する方法

import org.springframework.beans.factory.annotation.Value;

import org.springframework.stereotype.Component;

 

@Component

public class AppProperties {

 

@Value("${app.year:}")

    private String year;

 

public String getYear() {

return year;

}

 

public void setYear(String year) {

this.year = year;

}

}

 

上記の様にクラスに@Componentを、値に@Valueを指定すればよい。

値が未指定の場合、デフォルト値を指定したい場合は以下の様に記述する。

@Value("${app.year:2020}")

 

呼び出し方はAppPropetiesクラスを@Autowiredすればよい。