jar中的@PropertySource用于类路径上的外部文件

时间:2023-02-09 22:01:07

I'm trying to use the Spring framework's @PropertySource annotation in a Jar to load a properties file from outside the jar, but it's not finding the file.

我正在尝试在Jar中使用Spring框架的@PropertySource注释来从jar外部加载属性文件,但它没有找到该文件。

I need the properties file to be external to the Jar so it can be edited. I don't know the exact location where the file will be, I figured I could just have it anywhere on the classpath.

我需要将属性文件放在Jar的外部,以便进行编辑。我不知道文件的确切位置,我想我可以把它放在类路径的任何地方。

I'm using the following annotation on my Config class.

我在我的Config类上使用以下注释。

@PropertySource('classpath:stc.properties')

And placed stc.properties in the same directory as the created Jar file. I tried specifying the classpath explicitly in the java command, but it still cannot find the file:

并将stc.properties放在与创建的Jar文件相同的目录中。我尝试在java命令中显式指定类路径,但它仍然无法找到该文件:

java -cp . -jar stc.jar
Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to load bean class: com.example.stc.Config; nested exception is java.io.FileNotFoundException: class path resource [stc.properties] cannot be opened because it does not exist
        at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:162)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.processConfigBeanDefinitions(ConfigurationClassPostProcessor.java:299)
        at org.springframework.context.annotation.ConfigurationClassPostProcessor.postProcessBeanDefinitionRegistry(ConfigurationClassPostProcessor.java:243)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanDefinitionRegistryPostProcessors(PostProcessorRegistrationDelegate.java:254)
        at org.springframework.context.support.PostProcessorRegistrationDelegate.invokeBeanFactoryPostProcessors(PostProcessorRegistrationDelegate.java:94)
        at org.springframework.context.support.AbstractApplicationContext.invokeBeanFactoryPostProcessors(AbstractApplicationContext.java:609)
        at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:464)
[...]

Etc.

I've also tried using ./ as the classpath, and tried specifying the classpath (with both variants) in the Class-Path attribute of the jar's manifest, but it always gives the same results.

我也尝试使用./作为类路径,并尝试在jar的清单的Class-Path属性中指定类路径(包含两个变量),但它总是给出相同的结果。

5 个解决方案

#1


Assuming you have two files, one for local one for production

假设您有两个文件,一个用于本地生产

@PropertySources({
        @PropertySource("classpath:application.properties"),
        @PropertySource(value = "${ws.properties}", ignoreResourceNotFound = true)

})

And in tomcat or your jar file , pass on this parameter

在tomcat或jar文件中,传递此参数

-Dws.properties=file:/path-to.properties

I added this in setenv.sh

我在setenv.sh中添加了这个

APPLICATION_OPTS="-Dlog4j.configurationFile=file:$PATH/log4j2.xml -Dlog4j.debug=true -Dapplication.properties=file:$PATH/application.properties

APPLICATION_OPTS =“ - Dlog4j.configurationFile = file:$ PATH / log4j2.xml -Dlog4j.debug = true -Dapplication.properties = file:$ PATH / application.properties

This is possible with Spring 4 only

这仅适用于Spring 4

#2


Use a variable (System or Environment) to have the value of the file and you could refer your file like this:

使用变量(系统或环境)来获取文件的值,您可以像这样引用您的文件:

@PropertySource("file:${MY_PATH}/application.properties")

#3


My environment was:

我的环境是:

OS: Windows | Container: Tomcat | Java: 7 | Spring: 4.2.4 | Springboot 1.3.1 | Maven

操作系统:Windows |容器:Tomcat | Java:7 |春天:4.2.4 | Springboot 1.3.1 | Maven的

Step 1 a (war):

步骤1(战争):

Add the file externalised properties file to JVM system properties.

将文件外化属性文件添加到JVM系统属性。

As am running this off tomcat; I done this by creating setenv.bat in <TOMCAT_HOME>/bin/setenv.bat

正如我在tomcat上运行这个;我是通过在 /bin/setenv.bat中创建setenv.bat来完成的

set CATALINA_OPTS=%CATALINA_OPTS% -Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties

Step 1 b (jar):

步骤1 b(jar):

Alternative if you are running from a jar use:

如果您从jar运行使用替代方案:

-Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties

Note the use of file: at the start on the line.

注意使用file:在行的开头。

Step 2: In my application startup class I used annotation @PropertySource to load the specific environment application properties.

第2步:在我的应用程序启动类中,我使用注释@PropertySource来加载特定的环境应用程序属性。

@SpringBootApplication
@PropertySources({
        @PropertySource(value = "${external.app.properties.file}", ignoreResourceNotFound = true),
        @PropertySource(value = "classpath:application.properties")
})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

Step 3:

Using externalised properties in project

在项目中使用外化属性

external/file/path/application-prod.properties

spring.datasource.url.ext=< PRODUCTION_DATASOURCE >

spring.datasource.url.ext =

/src/main/resources/application.properties

spring.datasource.url=${spring.datasource.url.ext}

Hope this helps other having the same problem.

希望这有助于其他人遇到同样的问题。

#4


try giving the full path of the file:

尝试给出文件的完整路径:

@PropertySource('file:c:/.../stc.properties')

#5


you could use --spring.config.location=file:/somepath parameter when running jar, where you specify path to config file (could be relative).

你可以在运行jar时使用--spring.config.location = file:/ somepath参数,你可以在其中指定配置文件的路径(可以是相对的)。

More info in docs

更多信息在docs中

#1


Assuming you have two files, one for local one for production

假设您有两个文件,一个用于本地生产

@PropertySources({
        @PropertySource("classpath:application.properties"),
        @PropertySource(value = "${ws.properties}", ignoreResourceNotFound = true)

})

And in tomcat or your jar file , pass on this parameter

在tomcat或jar文件中,传递此参数

-Dws.properties=file:/path-to.properties

I added this in setenv.sh

我在setenv.sh中添加了这个

APPLICATION_OPTS="-Dlog4j.configurationFile=file:$PATH/log4j2.xml -Dlog4j.debug=true -Dapplication.properties=file:$PATH/application.properties

APPLICATION_OPTS =“ - Dlog4j.configurationFile = file:$ PATH / log4j2.xml -Dlog4j.debug = true -Dapplication.properties = file:$ PATH / application.properties

This is possible with Spring 4 only

这仅适用于Spring 4

#2


Use a variable (System or Environment) to have the value of the file and you could refer your file like this:

使用变量(系统或环境)来获取文件的值,您可以像这样引用您的文件:

@PropertySource("file:${MY_PATH}/application.properties")

#3


My environment was:

我的环境是:

OS: Windows | Container: Tomcat | Java: 7 | Spring: 4.2.4 | Springboot 1.3.1 | Maven

操作系统:Windows |容器:Tomcat | Java:7 |春天:4.2.4 | Springboot 1.3.1 | Maven的

Step 1 a (war):

步骤1(战争):

Add the file externalised properties file to JVM system properties.

将文件外化属性文件添加到JVM系统属性。

As am running this off tomcat; I done this by creating setenv.bat in <TOMCAT_HOME>/bin/setenv.bat

正如我在tomcat上运行这个;我是通过在 /bin/setenv.bat中创建setenv.bat来完成的

set CATALINA_OPTS=%CATALINA_OPTS% -Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties

Step 1 b (jar):

步骤1 b(jar):

Alternative if you are running from a jar use:

如果您从jar运行使用替代方案:

-Dexternal.app.properties=file:<PATH_TO_EXTERNAL_FILE>\application-prod.properties

Note the use of file: at the start on the line.

注意使用file:在行的开头。

Step 2: In my application startup class I used annotation @PropertySource to load the specific environment application properties.

第2步:在我的应用程序启动类中,我使用注释@PropertySource来加载特定的环境应用程序属性。

@SpringBootApplication
@PropertySources({
        @PropertySource(value = "${external.app.properties.file}", ignoreResourceNotFound = true),
        @PropertySource(value = "classpath:application.properties")
})
public class Application extends SpringBootServletInitializer {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }

}

Step 3:

Using externalised properties in project

在项目中使用外化属性

external/file/path/application-prod.properties

spring.datasource.url.ext=< PRODUCTION_DATASOURCE >

spring.datasource.url.ext =

/src/main/resources/application.properties

spring.datasource.url=${spring.datasource.url.ext}

Hope this helps other having the same problem.

希望这有助于其他人遇到同样的问题。

#4


try giving the full path of the file:

尝试给出文件的完整路径:

@PropertySource('file:c:/.../stc.properties')

#5


you could use --spring.config.location=file:/somepath parameter when running jar, where you specify path to config file (could be relative).

你可以在运行jar时使用--spring.config.location = file:/ somepath参数,你可以在其中指定配置文件的路径(可以是相对的)。

More info in docs

更多信息在docs中