Giter Club home page Giter Club logo

Comments (14)

MichaelSp avatar MichaelSp commented on May 24, 2024 5

Here is the same for java

    public static void main(String[] args) {
        Map<String, Object> env = Dotenv.load()
                .entries()
                .stream()
                .collect(
                        Collectors.toMap(DotenvEntry::getKey, DotenvEntry::getValue));
        new SpringApplicationBuilder(Application.class)
                .environment(new StandardEnvironment() {
                    @Override
                    protected void customizePropertySources(MutablePropertySources propertySources) {
                        super.customizePropertySources(propertySources);
                        propertySources.addLast(new MapPropertySource("dotenvProperties", env));
                    }
                }).run(args);
    }

from dotenv-kotlin.

tobiasbueschel avatar tobiasbueschel commented on May 24, 2024 3

Hi @cdimascio, thanks for your work on this project 👍

How can I use java-dotenv with a Spring project that uses an application.properties or application.yml file to configure server port, log level etc.?
https://docs.spring.io/spring-boot/docs/current/reference/html/common-application-properties.html

In such an application.yml, one can access environment variable using:

server:
  port: ${SERVER_PORT}

But this wouldn't find an environment variable if it's loaded locally using .env. One would need to run System.setProperty in the main method - are there other, perhaps cleaner ways to achieve the same?

System.setProperty("server.port", dotenv["SERVER_PORT"].toString())

from dotenv-kotlin.

cdimascio avatar cdimascio commented on May 24, 2024 3

Hmm, I think this may be a good opportunity to create a new Spring specific library that wraps java-dotenv to do something similar to what you described above.

It's likely not appropriate to bloat java-dotenv with framework specific logic, however I can certainly see significant value as a Spring user

from dotenv-kotlin.

n3v3rf411 avatar n3v3rf411 commented on May 24, 2024 3

I use the following (kotlin):

fun main(args: Array<String>) {
	val dotenv = dotenv().entries().associateByTo(hashMapOf(), {it.key}, {it.value}).toMap()

	SpringApplicationBuilder(ApiApplication::class.java)
		.environment(object : StandardEnvironment() {
			override fun customizePropertySources(propertySources: MutablePropertySources) {
				super.customizePropertySources(propertySources)
				propertySources.addLast(MapPropertySource("dotenvProperties", dotenv))
			}
		})
		.run(*args)
}

This will add the dotenv entries as a property source succeeding system and environment properties.

from dotenv-kotlin.

paulschwarz avatar paulschwarz commented on May 24, 2024 1

Please may I invite you to review https://github.com/paulschwarz/spring-dotenv which I believe addresses the need for a library as stated here cdimascio/java-dotenv#6 (comment)

@cdimascio I'd appreciate your input on this.

from dotenv-kotlin.

freshprinze avatar freshprinze commented on May 24, 2024 1

Please may I invite you to review https://github.com/paulschwarz/spring-dotenv which I believe addresses the need for a library as stated here #6 (comment)

@cdimascio I'd appreciate your input on this.

This is a nice wrapper. I am hoping to integrate it in a project I am working on. Thank You.

from dotenv-kotlin.

cdimascio avatar cdimascio commented on May 24, 2024

@lospejos I have an example using spring boot 2 with Kotlin and webflux functional. Hope it proves useful.
Repo: https://github.com/cdimascio/kotlin-swagger-spring-functional-template

Including dotenv as a global: https://github.com/cdimascio/kotlin-swagger-spring-functional-template/blob/master/src/main/kotlin/functional/Env.kt

You could instead use it as a bean, instead of a global
Let me know if this helps

from dotenv-kotlin.

cdimascio avatar cdimascio commented on May 24, 2024

Closing this out. @lospejos please reopen if you need something beyond what the examples above provide

from dotenv-kotlin.

cdimascio avatar cdimascio commented on May 24, 2024

@MichaelSp and @n3v3rf411 thanks so much for these examples.

If either of you is up to it, It would be fantastic to create a PR that makes these code snippets more accessible. Perhaps, create an examples folder at the root of this project, add these code snippets there, and provide a link to each from the examples section of the README.

from dotenv-kotlin.

MichaelSp avatar MichaelSp commented on May 24, 2024

I'd still prefer what you suggested. If I now created an PR with that, how would that contribute to the delay of your suggested solution? 😁

from dotenv-kotlin.

ptrckdev avatar ptrckdev commented on May 24, 2024

Thanks for the Java Code, very helpful.

Now I've got the problem that those variables are not getting used for my tests.
Not sure if my issue is getting my database credentials to the Test Class or having to change my Test Class to not use a complete spring server as those tests don't even need a DB connection 🤔

from dotenv-kotlin.

freshprinze avatar freshprinze commented on May 24, 2024

Thanks for the Java Code, very helpful.

Now I've got the problem that those variables are not getting used for my tests.
Not sure if my issue is getting my database credentials to the Test Class or having to change my Test Class to not use a complete spring server as those tests don't even need a DB connection

Having the same issue. I am thinking of using https://github.com/paulschwarz/spring-dotenv, by @paulschwarz as a fix. I will update how it works out.

from dotenv-kotlin.

sturufous avatar sturufous commented on May 24, 2024

Can I use spring-dotenv in a Java Spring Application that does not use Spring Boot? I have dotenv-java working fine, but spring-dotenv appears to do nothing.

from dotenv-kotlin.

paulschwarz avatar paulschwarz commented on May 24, 2024

@sturufous probably best to move this conversation to https://github.com/paulschwarz/spring-dotenv

In short, this is the hook that registers the property source https://github.com/paulschwarz/spring-dotenv/blob/master/library/src/main/resources/META-INF/spring.factories

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.