SpringBootでリソースパスを取得する方法は?

Spring Bootのリソースパスを取得する方法は複数あります。

  1. クラスパスリソース
  2. クラスパスリソース
  3. 設定ファイル.properties
ClassPathResource resource = new ClassPathResource("config.properties");
String path = resource.getFile().getAbsolutePath();
  1. リソースローダー
  2. リソースローダー
  3. リソースを取得する
  4. 設定ファイル.properties
@Autowired
private ResourceLoader resourceLoader;

public void getResourcePath() {
    Resource resource = resourceLoader.getResource("classpath:config.properties");
    String path = resource.getFile().getAbsolutePath();
}
  1. @値
  2. @値
  3. 設定ファイル「config.properties」
@Value("classpath:config.properties")
private Resource resource;

public void getResourcePath() {
    String path = resource.getFile().getAbsolutePath();
}

どのような方法を使用しても、クラスパスのリソースのパスを取得することができます。

bannerAds