如何将Spring ClassPathResource:与classpath:或classpath *:一起使用,以及是否以/开头? [英] How to use Spring ClassPathResource: with classpath: or classpath*: and leading / or not?

查看:216
本文介绍了如何将Spring ClassPathResource:与classpath:或classpath *:一起使用,以及是否以/开头?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在该位置有文件

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

其中

    --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();

但是我得到了FileNotFoundException,我尝试了

But I getting FileNotFoundException, I tried with

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

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

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

推荐答案

使用方式如下

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

这篇关于如何将Spring ClassPathResource:与classpath:或classpath *:一起使用,以及是否以/开头?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆