Closed VaishnavGhenge closed 5 months ago
i have the same problem. Can someone help me to solve this issue
The Dotenv module is load runtime, try using spring config to set DB creds in your main file.
import org.springframework.boot.jdbc.DataSourceBuilder;
...
@Bean
@Primary
DataSource dataSource() {
DataSourceBuilder<?> dataSourceBuilder = DataSourceBuilder.create();
Dotenv dotenv = Dotenv.configure().load();
dataSourceBuilder.driverClassName("org.postgresql.Driver");
dataSourceBuilder.url(dotenv.get("MAIN_DATASOURCE_URL"));
dataSourceBuilder.username(dotenv.get("MAIN_DATASOURCE_USERNAME"));
dataSourceBuilder.password(dotenv.get("MAIN_DATASOURCE_PASSWORD"));
return dataSourceBuilder.build();
}
I have added dependency in build.gradle implementation 'io.github.cdimascio:dotenv-java:2.3.2'
MyApplication.java
@SpringBootApplication public class MovieApiApplication { public static void main(String[] args) { Dotenv dotenv = Dotenv.load(); String myEnvVar = dotenv.get("MONGO_CLUSTER"); System.out.println(myEnvVar);
} } above code works but,
application.properties
spring.data.mongodb.database=${env:MONGO_DATABASE} spring.data.mongodb.uri=mongodb+srv://${env:MONGO_USER}:${env:MONGO_PASSWORD}@${env:MONGO_CLUSTER}
above code is not working as compiler is not able to resolve above env variables