タイトル: プロパティファイルの値や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すればよい。