Spring ClassPathResource-无法打开,因为它不存在 [英] Spring ClassPathResource - cannot be opened because it does not exist

查看:1335
本文介绍了Spring ClassPathResource-无法打开,因为它不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我仍将Artem Bilan的答案标记为正确,但我仍然觉得需要为以后的读者指出这一点.似乎我误解了@Value注释的默认值"的概念.我希望通过使用

UPDATE: I'll still keep Artem Bilan's answer marked as correct, but I still felt I needed to point this out for any future readers. It seems I was misunderstanding the concept of the 'default value' for the @Value annotation. The functionality I was hoping to achieve by using

@Value("${someProp:defaultFilePath}")
private Resource resourcefilePath;

是,如果application.properties中定义的someProp文件路径抛出异常(即找不到文件),则它将尝试使用defaultFilePath(即上述文件).定义默认值的实际作用是,如果属性本身(someProp)在application.properties文件中不存在(未定义或注释掉),则它将尝试改用默认值.

was that, should the file path someProp defined in application.properties throw an exception (i.e. file not found), it would then attempt to use the defaultFilePath (i.e. the one above). What defining a default value actually does is that, if the property itself (someProp) does not exist (not defined or commented out) in the application.properties file, it then attempts to use the default value instead.

我正在使用Spring Integration Sftp进行SSH文件传输.使用Spring Boot,所以没有xml文件.在配置DefaultSftpSessionFactory对象之前,我定义一个资源,其中包含一个.txt文件,该文件具有sFtp身份验证所需的私钥.

I'm using Spring Integration Sftp for SSH file transfer. Using Spring Boot so no xml files. Before configuring the DefaultSftpSessionFactory object, I define a resource which contains a .txt file that has the private key required for the sFtp authentication.

以前,我是这样使用FileSystemResource的:

Previously, I used FileSystemResource like this:

Resource resource = new FileSystemResource("C:/absolute/path/to/my/private/key/privateKey.txt");

这很好.但是,此应用程序最终将被放置在云环境中,这意味着类似的绝对路径将不再起作用.我试图改为使用ClassPathResource,但是不管我尝试什么,它都无法正常工作.到目前为止,我已经尝试了以下方法:

This worked just fine. However, this application will eventually be put in a cloud environment, which means absolute paths like that won't work anymore. I'm trying to instead use ClassPathResource but it's not working no matter what I try. So far I've tried the following:

Resource resource = new ClassPathResource("privateKey.txt");
Resource resource = new ClassPathResource("privateKey.txt", SomeClassInClassPath.class);
Resource resource = new ClassPathResource("com/my/package/name/privateKey.txt");

我的目录结构看起来像这样:

My directory structure looks something like this:

ProjectFolder -> src -> main -> java -> com -> my -> package -> name -> various java classes
                                                                        privateKey.txt
                             -> resources -> etc...

还有更多,但这是它的简化版本.谁能帮忙弄清楚我如何识别它的.txt路径?无论尝试如何,我都会不断得到java.io.FileNotFoundException: class path resource [resource] cannot be opened because it does not exist.

There's more but this is a simplified version of it. Can anyone help figure out how I can get it to recognize the path to my .txt? I keep getting java.io.FileNotFoundException: class path resource [resource] cannot be opened because it does not exist no matter what I try.

WAR结构:

ProjectFolder -> META-INF -> maven -> etc..
              -> org -> springframework -> boot -> etc..
              -> WEB-INF -> classes -> com
                                    -> public
                                    -> application.properties
                                    -> privateKey.txt

推荐答案

您显示src -> main ->等,但这与运行时无关.

You show src -> main -> etc., but that doesn't matter for runtime.

如果您真的要在最终应用程序中保存该文件(jar?war?),请确保将文件打包为最终应用程序的一部分.

If you really are going to have that file in the final application (jar? war?), be sure that you pack that file really as a part of the final application.

并在此处共享该结构.

使用Spring Java&注释配置,您无需担心ClassPathResource对象.在适当的@Configuration中足以这样声明它:

With Spring Java & Annotation Configuration you don't need to worry about ClassPathResource object. There is just enough to declare it like this in the appropriate @Configuration:

@Value("com/my/package/name/privateKey.txt")
private Resource privateKey;

这篇关于Spring ClassPathResource-无法打开,因为它不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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