简体   繁体   English

jar外部的Spring Boot外部属性

[英]Spring Boot External properties outside of jar

I am creating a Spring Boot application and i faced this problem. 我正在创建一个Spring Boot应用程序,我遇到了这个问题。
I have my application.properties in recource folder but i also need external.properties file outside of jar to configure properties such like: 我将我的application.properties在资源文件夹中,但我还需要jar外部的external.properties文件来配置诸如以下属性:
name, 名称,
password, 密码,
etc. 等等
I want to have external.properties file outside of jar and inside resources folder for testing while developing. 我想在jar外部和resources文件夹中拥有external.properties文件,以便在开发时进行测试。
I tried creating configuration file like this: 我试过像这样创建配置文件:

@Configuration
 @PropertySource("classpath:" + SpringConfiguration.EXTERNALIZED_PROPERTIES)
 @PropertySource(value = "file:./" + 
 SpringConfiguration.EXTERNALIZED_PROPERTIES, ignoreResourceNotFound = true)
 public class SpringConfiguration {
       static final String EXTERNALIZED_PROPERTIES = "external.properties";
 }

But it still reads properties from resource folder. 但是它仍然从资源文件夹读取属性。 How can i make it read from outside of jar? 我怎样才能从罐子外面读取它?

Try specifying an absolute system path as the value of file: attribute. 尝试将绝对系统路径指定为file:属性的值。

Optionally I would advise setting that absolute path first as an ENV variable and then using that variable in the file: : 可选地,我建议先将绝对路径设置为ENV变量,然后在file:使用该变量:

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

So that when that directory changes you wont need to alter your code. 这样,当该目录更改时,您无需更改代码。

If you want different props for development and production, use application-dev.properties and application-prod.properties and set proper spring profile on startup. 如果您需要不同的开发和生产道具,请使用application-dev.properties和application-prod.properties并在启动时设置适当的spring配置文件。 If you need to override any property from the jar just add -Dmyproperty=myvalue to startup command 如果您需要覆盖jar中的任何属性,只需在启动命令中添加-Dmyproperty=myvalue

Similar answer of Maciej Kowalski. Maciej Kowalski的类似答案。

@PropertySources({
@PropertySource(value = {"classpath:application.properties"}, ignoreResourceNotFound = true),
@PropertySource(value = {"file:${external.config.location}/application.properties"}, ignoreResourceNotFound = true)

}) })

Assuming in folder " /home/me/configs " you have " application.properties " file. 假设在文件夹“ / home / me / configs ”中,您具有“ application.properties ”文件。

Run app with custom folder: 使用自定义文件夹运行应用程序:

java -jar app.jar --external.config.location="/home/me/configs"

or 要么

java -jar app.jar --external.config.location="C:\\users\\your_user\\configs"

Additionally you could export it as environment variable (unix) 另外,您可以将其导出为环境变量(unix)

export JAVA_OPTS='-Dexternal_config_location=/home/me/configs'

If you need to add external properties you can specify that in application.properties only. 如果需要添加外部属性,则只能在application.properties指定。

For example: myapplication.username='john' 例如: myapplication.username='john'

From Spring boot code you can access it like: 从Spring引导代码中,您可以像这样访问它:

 @Autowired  
 private Environment env;

//To access it
 String username = env.getProperty("myapplication.username");  

OR 要么

@Value("$myapplication.username")

I don't think there is any need to have external file if your requirement is as you mention in question. 如果您的要求是您所提到的,我认为不需要外部文件。

The solution was to delete external.properties and configuration file. 解决的办法是删除external.properties和配置文件。 And instead of using it put all properties to application.properties. 而不是使用它,而是将所有属性放到application.properties中。 And put application.properties to folder with jar. 并将application.properties放入jar文件夹。 Spring automatically prioritizes this property file over the property file inside jar. Spring会自动将这个属性文件的优先级设置为高于jar中的属性文件的优先级。

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

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