简体   繁体   English

加载资源文件,如(sql、txt 等)与 Spring ApplicationContext 类路径的绝对路径

[英]Load resource file like (sql, txt etc) with absolute path to Spring ApplicationContext classpath

Usually in a maven based spring application, if I need to make use of any file resource I would usually place it in a folder src/main/resources and then it would magically be available for spring app config file as shown below with a file reference such as "classpath:create-table.sql".通常在基于 maven 的 spring 应用程序中,如果我需要使用任何文件资源,我通常会将它放在文件夹 src/main/resources 中,然后它将神奇地可用于 spring 应用程序配置文件,如下所示,并带有文件引用例如“classpath:create-table.sql”。

spring_config.xml spring_config.xml

<jdbc:embedded-database id="dataSource" type="DERBY">
    <jdbc:script location="classpath:create-table.sql" />
</jdbc:embedded-database>

It is even more magical if the application context is being loaded from web.xml with WebApplicationContext in which case it won't even need the following style of initialization for application context, but if I were to write a standalone java based spring application, which needs the following app context initialization and not maven or web based, how do I make any resource file available in it's classpath to refer such a file(create-table.sql) as shown above from with in config file?如果应用程序上下文是从带有 WebApplicationContext 的 web.xml 加载的,那么它甚至不需要应用程序上下文的以下初始化样式,但如果我要编写一个独立的基于 java 的 spring 应用程序,它就更神奇了需要以下应用程序上下文初始化而不是基于 maven 或 web,我如何在它的类路径中使用任何资源文件来引用这样的文件(create-table.sql),如上所示从配置文件中?

ApplicationContext context = new ClassPathXmlApplicationContext("/spring_config.xml");

I know how to load with an absolute path such as shown below but what I need is to be able to make resource file available in classpath for the spring config file to refer from classpath itself.我知道如何使用如下所示的绝对路径加载,但我需要的是能够在类路径中提供资源文件,以便 spring 配置文件从类路径本身引用。

<jdbc:embedded-database id="dataSource" type="DERBY">
    <jdbc:script location="file:C:\junit_framework\springmodulecontext\create-table.sql" />
</jdbc:embedded-database>

All the more better if there is a way to refer the file with a relative path, so that I could just place the resource file right next to config file and refer it with a current directory notation such as "file:./create-table.sql"?如果有一种方法可以使用相对路径引用文件,那就更好了,这样我就可以将资源文件放在配置文件旁边,并使用当前目录符号来引用它,例如“file:./create-table .sql”? I tried this absolute notation but it doesn't seem to work.我尝试了这种绝对符号,但似乎不起作用。

if I were to write a standalone java based spring application and not maven or web based, I make any resource file available in classpath by including its directory path (absolute or relative) to java -classpath list.如果我要编写一个独立的基于 java 的 spring 应用程序,而不是基于 maven 或 web 的,我会通过将其目录路径(绝对或相对)包含到 java -classpath 列表来使任何资源文件在类路径中可用

That is to say, if I want a file reference such as "classpath:create-table.sql" to actually read ./create-table.sql, I need to put a .也就是说,如果我想要一个像“classpath:create-table.sql”这样的文件引用来实际读取./create-table.sql,我需要把一个. (which is directory path of a desired file) to classpath like: (这是所需文件的目录路径)到类路径,如:

java -classpath .:lib/*.jar:... java -classpath .:lib/*.jar:...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM