I have a spring boot application which interacts with DB to provide resource using Spring data Rest. I want to get the configuration from environment variables. Below is my properties file.
spring.datasource.url=${mysql.url}
spring.datasource.username=${mysql.user}
spring.datasource.password=${mysql.password}
And my environment variables are in the image https://ibb.co/cyxsNc
I even tried with the below config too
spring.datasource.url=${MySQL_Url}
spring.datasource.username=${MySQL_User}
spring.datasource.password=${MySQL_Password}
But I am not able to connect to the DB and getting the below error
org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Tomcat.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.tomcat.jdbc.pool.DataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalArgumentException: URL must start with 'jdbc'
Application folder structure
Project
|-- src/main/java
|-- com.example.app
|-- DemoApplication.java
|-- src/main/resources
|-- application.properties
Note: The configuration works fine if I set the values like below
spring.datasource.url=jdbc:mysql://localhost:3306/ex_man
spring.datasource.username=root
spring.datasource.password=root
What am I missing?
Spring Boot allows you to externalize your configuration so you can work with the same application code in different environments. You can use properties files, YAML files, environment variables and command-line arguments to externalize configuration.
Spring @Configuration annotation is part of the spring core framework. Spring Configuration annotation indicates that the class has @Bean definition methods. So Spring container can process the class and generate Spring Beans to be used in the application.
Check out this documentation here: https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
try naming your environment variables:
SPRING_DATASOURCE_URL
SPRING_DATASOURCE_USERNAME
SPRING_DATASOURCE_PASSWORD
UPDATE:
Spring boot does properly pick up the environment variables, see test below.
package com.example.environment_vars;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import java.util.Map; // Import this for systemEnvironment
@SpringBootApplication
public class EnvironmentVarsApplication {
@Value("#{systemEnvironment['ENV_VAR'] ?: 'Default_value'}")
private String envVar;
@Bean
public CommandLineRunner commandLineRunner() {
return new CommandLineRunner() {
@Override
public void run(String[] arg0) throws Exception {
System.out.println(envVar);
}
};
}
public static void main(String[] args) {
SpringApplication.run(EnvironmentVarsApplication.class, args);
}
}
This will print out the value of the environment variable ENV_VAR and if the value is not present, it will print the Default_Value.
@Value
injects the value accessible throughout the project.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With