Get Resource Path in Spring Boot
There are multiple ways to obtain the resource path in Spring Boot.
- A resource located on the classpath
- Resource located in the classpath.
- configuration properties file
ClassPathResource resource = new ClassPathResource("config.properties");
String path = resource.getFile().getAbsolutePath();
- Tool for loading resources
- Loader of resources
- obtain resource
- configuration properties
@Autowired
private ResourceLoader resourceLoader;
public void getResourcePath() {
Resource resource = resourceLoader.getResource("classpath:config.properties");
String path = resource.getFile().getAbsolutePath();
}
- Assign value
- @Amount
- 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.