SpringBootでリソースパスを取得する方法は?
Spring Bootのリソースパスを取得する方法は複数あります。
- クラスパスリソース
- クラスパスリソース
- 設定ファイル.properties
ClassPathResource resource = new ClassPathResource("config.properties");
String path = resource.getFile().getAbsolutePath();
- リソースローダー
- リソースローダー
- リソースを取得する
- 設定ファイル.properties
@Autowired
private ResourceLoader resourceLoader;
public void getResourcePath() {
Resource resource = resourceLoader.getResource("classpath:config.properties");
String path = resource.getFile().getAbsolutePath();
}
- @値
- @値
- 設定ファイル「config.properties」
@Value("classpath:config.properties")
private Resource resource;
public void getResourcePath() {
String path = resource.getFile().getAbsolutePath();
}
どのような方法を使用しても、クラスパスのリソースのパスを取得することができます。