在类路径上添加属性文件 [英] Add properties file on classpath

查看:299
本文介绍了在类路径上添加属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建一个基于Spring Boot的Spring独立应用程序。我希望此应用程序从一个属性文件中读取其属性,该文件位于单独目录中的jar文件之外。构建的应用程序的结构如下所示

I am building a Spring standalone application which is based on Spring Boot. I want this application to read its properties from a property file which is outside of jar file in a separate directory. The structure of the built application looks like this

testApplication
├── test-1.0-SNAPSHOT.jar
├── lib
│   └── <dependencies are here>
└── conf
    └── application.properties

我想要在classpath上加载application.properties文件,这样我就能读入我的应用程序。可能吗?因为当我像这样运行我的jar文件时(在Windows上):

I want to load the application.properties file on classpath, so I am able to read in my application. Is it possible? Because when I run my jar file like this (on Windows):

java -cp "./*;lib/*;conf/*" com.myapp.Test

我得到以下例外:

Exception in thread "main" org.springframework.beans.factory.BeanDefinitionStoreException: Failed to parse configuration class [com.myapp.Test]; nested exception is java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist
    at org.springframework.context.annotation.ConfigurationClassParser.parse(ConfigurationClassParser.java:180)
...
Caused by: java.io.FileNotFoundException: class path resource [application.properties] cannot be opened because it does not exist       
    at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:172)

我尝试通过此类注释读取应用程序中的文件:

I try to read the file from application by this Class annotation:

@PropertySource("classpath:application.properties")

Java文档没有提到,可以在类路径上加载非jar文件,但是如果这个application.properties在jar文件test-1.0-SNAPSHOT.jar中,然后可以通过这种方式访问​​它。

Java documentation does not mention, that a non-jar files can be loaded on classpath, but if this application.properties is in the jar file test-1.0-SNAPSHOT.jar, then it can be accessed by this way.

是否可以在classpath上放置外部非jar文件?我知道我不一定有类路径上的文件,我可以通过不同的方式访问它,但我仍然很好奇是否可能。

Is it possible to put external non-jar file on classpath? I know I don't necessarily have the file on classpath, I can access it by different ways but still I am curious if it is possible.

推荐答案

尝试

java -cp "./*;lib/*;conf" com.myapp.Test

Asterix(*)用于查找所有JAR而不是类。

Asterix (*) is used to find all JARs not classes.

这篇关于在类路径上添加属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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