英文:
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 "/locationFiles"
value for example. myproperty.properties
file location is "/locationFiles/config"
and myLogFile.log
file location is "/locationFiles/log"
.
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("MYENV")+"/config/";
}
}
The following code snippet does not match with what I want to do as I cannot
write something like that : @PropertySource(env.getProperty("MYENV")+"/config/")
@SpringBootApplication
@PropertySource("classpath:myproperty.properties")
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=<path-to-file> 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("file:${MYENV}/config/my.properties")
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论