在Spring Boot中获取resource路径有多种方式:
- 使用
ClassPathResource类:可以通过ClassPathResource类获取classpath下的资源路径。例如,可以使用以下代码获取classpath下的config.properties文件路径:
ClassPathResource resource = new ClassPathResource("config.properties");
String path = resource.getFile().getAbsolutePath();
- 使用
ResourceLoader接口:通过注入ResourceLoader接口,可以使用getResource方法获取资源路径。例如,可以使用以下代码获取classpath下的config.properties文件路径:
@Autowired
private ResourceLoader resourceLoader;
public void getResourcePath() {
Resource resource = resourceLoader.getResource("classpath:config.properties");
String path = resource.getFile().getAbsolutePath();
}
- 使用
@Value注解:可以使用@Value注解将资源路径注入到变量中。例如,可以使用以下代码获取classpath下的config.properties文件路径:
@Value("classpath:config.properties")
private Resource resource;
public void getResourcePath() {
String path = resource.getFile().getAbsolutePath();
}
无论使用哪种方式,都可以获取到classpath下资源的路径。
提供PHP及ThinkPHP框架的定制开发、代码优化,PHP修改、ThinkPHP修改。
邮箱:yvsm@163.com
微信:yvsm316
QQ:316430983
版权声明:除特别声明外,本站所有文章皆是本站原创,转载请以超链接形式注明出处!