dotenv-kotlin icon indicating copy to clipboard operation
dotenv-kotlin copied to clipboard

Making dotenv variables globally available

Open jessevdp opened this issue 6 years ago • 5 comments

I was wondering if there was any way to make the variables in the .env file globally available throughout my entire java application.

It seems as though configuring and loading the variables returns a Map of sorts from which you can retrieve all of the values. However I have to configure and load this variable in every file from which I wish to use it.

Dotenv env = Dotenv.configure().directory("./").load();

Is there any way I can load them once and use them everywhere? Perhaps even through the native System.getenv() static method? Running the configuration and loading once from my public static main method or something.

jessevdp avatar Apr 11 '18 20:04 jessevdp

Currently, it's not available statically, though its worth consideration.

One way to simulate the behavior you are looking for, would be to assign Dotenv to a public static variable. e.g.

public static Dotenv ENV = Dotenv.configure().load();

Then can call e.g. ENV.get("MY_EV") throughout your app

cdimascio avatar Apr 23 '18 18:04 cdimascio

Hmm yes that does make sense. I was mainly looking for this option because (as you might be able to tell from my other issues: #4, #3) on our project we're going to need some configuration. Having to do this config in every class where the variables are needed seems like it could cause for some issues where one part of the setup is updated but the rest isn't.

I had seen some other dotenv type of project out there for Java where the api was something like "load once, then use the System.getenv() method all around. (Can't seem to find it at the moment though ☹️)

But I could also abstract this whole dotenv setup into my own utility class where I do the config and make the variables statically available.

jessevdp avatar Apr 24 '18 05:04 jessevdp

@jessevdp System.getenv() is not an option with Java due to do the inability to set an env var on the running process. I describe it in the FAQ section. All in all, any dotenv implementation that does this, may not be guaranteed to run properly on all JVMs. All in all, I plan to keep this ticket open and consider a static helper as you proposed

For now, the the method I recommended above is one possible approach to get ev's via a static helper within your app.

cdimascio avatar Apr 24 '18 20:04 cdimascio

I build my own little util class if you're interested. I used this class to normalise the configuration for Dotenv throughout my app too.

import io.github.cdimascio.dotenv.Dotenv;

public class Config {
	private static final Config instance = new Config();
	
	public static Config getConfig () {
		return instance;
	}
	
	private Dotenv env;
			
	public Config () {
		this.env = Dotenv.configure().directory("./").ignoreIfMissing().load();
	}
	
	public String get (String variable) {
		return env.get(variable);
	}
}

jessevdp avatar Apr 25 '18 11:04 jessevdp

@jessevdp are you using a framework? If not, your Config class might have to do. But if yes, you might want to find a way to fit dotenv into there in a properly integrated fashion. Here is my example using Spring https://github.com/paulschwarz/spring-dotenv

Beyond a simple static helper, I'm not sure that java-dotenv should do too much.

paulschwarz avatar Mar 26 '20 18:03 paulschwarz