Get Resource Path in Spring Boot

There are multiple ways to obtain the resource path in Spring Boot.

  1. A resource located on the classpath
  2. Resource located in the classpath.
  3. configuration properties file
ClassPathResource resource = new ClassPathResource("config.properties");
String path = resource.getFile().getAbsolutePath();
  1. Tool for loading resources
  2. Loader of resources
  3. obtain resource
  4. configuration properties
@Autowired
private ResourceLoader resourceLoader;

public void getResourcePath() {
    Resource resource = resourceLoader.getResource("classpath:config.properties");
    String path = resource.getFile().getAbsolutePath();
}
  1. Assign value
  2. @Amount
  3. properties configuration file
@Value("classpath:config.properties")
private Resource resource;

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

You can obtain the path of resources under the classpath using any method.

bannerAds