0

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 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 Spring boot how to read properties file outside jar. I want to know if there is an alternative way to this method.

cknelle
  • 131
  • 1
  • 12

1 Answers1

0

thanx to How to use system environment variable as part of @PropertySource value? link I found out my answer using @PropertySource("file:${MYENV}/config/my.properties")

cknelle
  • 131
  • 1
  • 12