Comments (14)
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.
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.
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.
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.
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.
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.
@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.
Closing this out. @lospejos please reopen if you need something beyond what the examples above provide
from dotenv-kotlin.
@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.
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.
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.
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.
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.
@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)
- Dotenv entries() result is not overridden by environment variables HOT 3
- Not working correctly when I execute a jar after gradle build HOT 2
- Option to only load .env file env vars HOT 6
- Multiline value HOT 8
- using env vars from host, it requires .env file HOT 1
- Directory configuration not working correctly HOT 3
- question - incorporating into build.gradle HOT 5
- 5.1.3 library version does not resolve correctly .env file location in jar file. HOT 3
- How is it different from Spring properties? HOT 5
- Make dotenv env variables available in System.getProperty() too HOT 6
- .env file and Travis HOT 5
- Getting an error when running a jar that uses dotenv HOT 1
- kotlin libs are required HOT 3
- Update version in readme
- Crashes on Android version 7 and older HOT 2
- package version 6.3.0 is not published anywhere other than github packages HOT 1
- Android Could not find /asset/env on the classpath HOT 1
- Errors when attempting to start it from a jar.
- Latest build on maven HOT 1
- Feature missing in docs.. DECLARED_IN_ENV filter
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dotenv-kotlin.