クラスパス上のプロパティファイルを読み込む標準的な方法であるリソースバンドルを使用することをお勧めします(特定のファイルでも使用できます)。
リソースバンドルは、通常、キー値のペアを格納するファイルで使用されます。
コードサンプル:
ResourceBundle bundle = ResourceBundle.getBundle("config"); //creates bundle from file `config.properties`
List<String> keys = (List<String>) bundle.getKeys(); //gets the list of keys found in the bundle (file)
String value = bundle.getString("gMapReportUrl"); //gets the value for the key `mykey`
config.properties:あなたはできるはずですこれらの方法の組み合わせを使用して
gMapReportUrl = someValueの
someOtherKey =ランダム
あなたが必要とするすべてのデータをプロから入手するpertyファイル。ここにあなたの新しいプロパティは、名前が「otherprops.properties」で、プロパティ名が「がmyName」であるファイル
@PropertySource(ignoreResourceNotFound = true, value = "classpath:otherprops.properties")
@Controller
public class ClassA {
@Value("${myName}")
String name;
@RequestMapping(value = "/xyz")
@ResponseBody
public void getName(){
System.out.println(name);
}
}
:
なぜfooは無効ですか?このクラスの使い方は? – gstackoverflow