Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Making dotenv variables globally available #2

Open
jessevdp opened this issue Apr 11, 2018 · 5 comments
Open

Making dotenv variables globally available #2

jessevdp opened this issue Apr 11, 2018 · 5 comments

Comments

@jessevdp
Copy link

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.

@cdimascio
Copy link
Owner

cdimascio commented Apr 23, 2018

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 cdimascio self-assigned this Apr 23, 2018
@jessevdp
Copy link
Author

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.

@cdimascio
Copy link
Owner

cdimascio commented Apr 24, 2018

@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.

@jessevdp
Copy link
Author

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);
	}
}

@paulschwarz
Copy link
Contributor

@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.

@cdimascio cdimascio removed their assignment Jun 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants