Java 如何使用 Spring ClassPathResource: with classpath: 或 classpath*: 并引导 / 或不引导?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36588915/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:28:01  来源:igfitidea点击:

How to use Spring ClassPathResource: with classpath: or classpath*: and leading / or not?

javaspring

提问by Suganthan Madhavan Pillai

I having file in the location

我在该位置有文件

--src
  --> main
   --> config
    --> application
     --> context
      --> reference
       --> user
        --> user.xml

where

在哪里

    --src
      --> main
       --> config

is in the classpath. Now I am trying to access the file using

在类路径中。现在我正在尝试使用

Resource resource = new ClassPathResource("classpath**:/application/context/references/user/user.xml");
File file = resource.getFile();

But I getting FileNotFoundException, I tried with

但我得到了FileNotFoundException,我试过

Resource resource = new ClassPathResource("classpath:/application/context/references/user/user.xml");
File file = resource.getFile();

too, but still I getting the exception. Can someone help me to understand the working of ClassPathResourceand right solution?

也是,但我仍然得到例外。有人可以帮助我了解ClassPathResource正确的解决方案的工作原理吗?

采纳答案by KayV

Use as below

使用如下

Resource resource = new ClassPathResource("/application/context/references/user/user.xml");
File file = resource.getFile();