How can I set my own property file and log file location in spring boot through my own environment variable?

huangapple go评论57阅读模式
英文:

How can I set my own property file and log file location in spring boot through my own environment variable?

问题

以下是您要求的代码部分的翻译:

我想通过自己的环境变量名例如 `MYENV`)来设置属性文件(`myproperty.properties`)和日志文件位置(`myLogFile.log`)。
属性文件名必须与 Spring Boot 的 `application.properties` 名称不同日志文件也有自己的名称
不想使用 `spring.config.name``spring.config.location`。

`MYENV` 将设置为 `"/locationFiles"`,例如 `myproperty.properties` 文件位置是 `"/locationFiles/config"`,
`myLogFile.log` 文件位置是 `"/locationFiles/log"`。

我知道我可以使用以下代码片段来读取我的环境变量
但是如何使用下面的 `propertiesLocation` 来以简单的 Spring Boot 方式读取属性数据呢
我不知道如何定义相应的Java配置类因为配置ppties文件路径似乎无法设置为变量

```java
import org.springframework.core.env.Environment;

public class MyClass {

	@Autowired
	private Environment env;
	
	String propertiesLocation;
	
	private void propertyLocation() {
	this.propertiesLocation = env.getProperty("MYENV") + "/config/";

	}
	
}

以下代码片段不符合我的要求,因为我无法编写类似这样的代码:@PropertySource(env.getProperty("MYENV") + "/config/")

@SpringBootApplication
@PropertySource("classpath:myproperty.properties")
public class MyApplication {

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

}

我看到了 https://stackoverflow.com/questions/32196451/environment-specific-application-properties-file-in-spring-boot-application,但它与我上面描述的情况不完全匹配。
因为我想定义自己的环境变量名称和文件名称。
我也在寻找另一种方法,而不是像在 https://stackoverflow.com/questions/41754459/spring-boot-how-to-read-properties-file-outside-jar 中所定义的那样使用 java -jar -Dspring.config.location=<path-to-file> myBootProject.jar
我想知道是否有替代方法来实现这个目标。

英文:

I want to set both property file (myproperty.properties) and log file location (myLogFile.log) through my own environment variable name (MYENV for example).
property file name must be different from spring boot application.properties name and log file has its own name also.
Do not want to use spring.config.name and spring.config.location.

MYENV will be set to &quot;/locationFiles&quot; value for example. myproperty.properties file location is &quot;/locationFiles/config&quot;
and myLogFile.log file location is &quot;/locationFiles/log&quot;.

I know that I can use the following code snippet for reading my environment variable.
But How do I use propertiesLocation below to read the properties data in a simple Spring boot way?
I do not know how to define a corresponding java configuration class as It seems that configuration ppties file path cannot be set in a variable.

import org.springframework.core.env.Environment;

public class MyClass {

	@Autowired
	private Environment env;
	
	String propertiesLocation;
	
	private void propertyLocation() {
	this.propertiesLocation = env.getProperty(&quot;MYENV&quot;)+&quot;/config/&quot;;

	}
	
}

The following code snippet does not match with what I want to do as I cannot
write something like that : @PropertySource(env.getProperty(&quot;MYENV&quot;)+&quot;/config/&quot;)

@SpringBootApplication
@PropertySource(&quot;classpath:myproperty.properties&quot;)
public class MyApplication {

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

}

I saw <https://stackoverflow.com/questions/32196451/environment-specific-application-properties-file-in-spring-boot-application> but I does not match exactly with what I've described above.
As I want to define my own environment variable name and file names.
And I'm also looking for another way than using java -jar -Dspring.config.location=&lt;path-to-file&gt; myBootProject.jar as defined in <https://stackoverflow.com/questions/41754459/spring-boot-how-to-read-properties-file-outside-jar>.
I want to know if there is an alternative way to this method.

答案1

得分: 0

感谢https://stackoverflow.com/questions/26387645/how-to-use-system-environment-variable-as-part-of-propertysource-value的链接,我发现了我的答案,使用@PropertySource("file:${MYENV}/config/my.properties")

英文:

thanx to https://stackoverflow.com/questions/26387645/how-to-use-system-environment-variable-as-part-of-propertysource-value link
I found out my answer using @PropertySource(&quot;file:${MYENV}/config/my.properties&quot;)

huangapple
  • 本文由 发表于 2023年2月7日 00:53:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75364283.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定