Giter Club home page Giter Club logo

Comments (5)

jessevdp avatar jessevdp commented on May 27, 2024 1

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

from dotenv-kotlin.

cdimascio avatar cdimascio commented on May 27, 2024

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

from dotenv-kotlin.

jessevdp avatar jessevdp commented on May 27, 2024

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.

from dotenv-kotlin.

cdimascio avatar cdimascio commented on May 27, 2024

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

from dotenv-kotlin.

paulschwarz avatar paulschwarz commented on May 27, 2024

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

from dotenv-kotlin.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤ī¸ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.