在Spring项目中,可以通过以下方式获取resources文件夹的路径地址:\n\n1. 使用ClassPathResource类:\n\njava\nimport org.springframework.core.io.ClassPathResource;\n\npublic class ResourcePathExample {\n\n public static void main(String[] args) {\n ClassPathResource resource = new ClassPathResource("");\n String path = resource.getPath();\n System.out.println("Resource path: " + path);\n }\n}\n\n\n上述代码中,创建了一个ClassPathResource对象,通过调用getPath()方法获取资源文件夹的路径地址。\n\n2. 使用ResourceLoader类:\n\njava\nimport org.springframework.core.io.ResourceLoader;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.stereotype.Component;\n\n@Component\npublic class ResourcePathExample {\n\n @Autowired\n private ResourceLoader resourceLoader;\n\n public void getResourcePath() {\n String path = resourceLoader.getResource("").getURI().toString();\n System.out.println("Resource path: " + path);\n }\n}\n\n\n在上述代码中,通过使用@Autowired注解将ResourceLoader注入到ResourcePathExample类中,并通过调用getResource("")方法获取资源文件夹的路径地址。\n\n无论是使用ClassPathResource类还是ResourceLoader类,都可以获取resources文件夹的路径地址。

Spring项目中获取resources文件夹路径地址的两种方法

原文地址: https://www.cveoy.top/t/topic/p9Bc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录