How to use Eclipse Variables in Spring Boot application.properties - java

I'm very new to Spring boot and I'm setting up my DB connection in then application.properties. Since I'd like to upload my project on GitHub I'm trying to access the datasource id and password from enviroment variables, instead of hardcoding them inside the properties file. But I'm not really sure about how to do it.
I'm using a Sql server instance running in a container, and I've tried the following:
In "run configuration", in the Enviroment Tab, I added 2 enviroment variables : DB_USERNAME and DB_PASSWORD
The application.properties looks like this:
spring.datasource.username=${DB_USERNAME)
spring.datasource.password=${DB_PASSWORD)
But when i do run the spring boot application i get the following error:
com.microsoft.sqlserver.jdbc.SQLServerException: Login failed for user '${DB_USERNAME)'
Can you help me figure out how to use the eclipse enviroment variables for this purpose in the right way?

Looks like there is a typo there:
spring.datasource.username=${DB_USERNAME)
spring.datasource.password=${DB_PASSWORD)
Should actually be:
spring.datasource.username=${DB_USERNAME}
spring.datasource.password=${DB_PASSWORD}
You can also specify default that would be applied if variables are not provided:
spring.datasource.username=${DB_USERNAME:username}
spring.datasource.password=${DB_PASSWORD:password}

Related

Using logging_level as environment parameter name in application.yml triggers building error

One of our spring-boot projects is using application.yml, we set the logging level as below:
logging:
level:
root: ${logging_level}
org.springframework.cloud.gateway: ${logging_level}
The ${logging_level}'s value is injected via the environment variable by a ConfigMap; however, it failed.
application.yml error msg-1
application.yml error msg-2
After searching, I guess the logging_level is converted to logging.level and for spring-boot 2 which needs a logger name.
When I set the same property in another project's application.properties, it works well.
Code as below:
logging.level.root=${logging_level}
I am so confused about why the parameter's value providers are same, but it ran well in the application.properties but failed in application.yml, could anyone help provide some suggestions?
Thank you very much!

ERROR: Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured

I've seen the other related posts for this topic, with none having an explanation for my problem unless I'm missing something. So, I'm developing a spring boot web app with 2 other developers. They are able to run the app locally, generate a war file, and connect to their local databases with no issues. However, I for some reason am not able to due to the following error report when I run the war file:
Description:
Failed to configure a DataSource: 'url' attribute is not specified and no embedded datasource could be configured.
Reason: Failed to determine a suitable driver class
Action:
Consider the following:
If you want an embedded database (H2, HSQL or Derby), please put it on the classpath.
If you have database settings to be loaded from a particular profile you may need to activate it (no profiles are currently active).
I am able to run a ./gradlew clean build with a successful build, but I fail on ./gradlew bootRun. This is a gradle project, where we are using the following flyway configuration to create our user, password, and db connection:
flyway {
url = System.getenv('HW_DATABASE_URL')
user = System.getenv('HW_DATABASE_USER')
password = System.getenv('HW_DATABASE_PASSWORD')
}
I then check the .bashrc file, which is what I'm sourcing these values with, and each of the 3 fields has the correct values that I created. And then I run an env | grep HW command to confirm that the values do show up on the command line and are the ones being used, and they correctly do.
Here is the .bashrc file without the values being shown:
Another interesting note is that all of this was working and I was able to run the app on my Mac prior to updating to Big Sur. After the update, this became the issue.
Any ideas from anyone here? Would be much appreciated.

Set user defined environment variables for spring boot webapp deployed in apache tomcat container

I have a spring boot app hosted on AWS Ubuntu instance, deployed using WAR deployment from Jenkins, in Apache tomcat(8.5.14) container.
I have one environment variable defined in application-test.properties file as below-
drehr.config.upload.image = /home/ubuntu/ehrFiles/
(application-test.properties file is used because active profile is set to test)
My java code accesses this property using following syntax:-
import org.springframework.beans.factory.annotation.Value;
#Value("${drehr.config.upload.image}")
private String imagePath;
But I am unable to get its value in imagePath variable, it is coming blank and the code is failing. Please provide some way to the solution for this.
Also tried changing the property value inside apache-tomcat-x/webapps/drehrweb-0.0.1/WEB-INF/classes/application-test.properties. Still doesn't work.
The strange thing is the same code works fine on development environment(using spring tool suite).

Spring can't locate a driver class that *is* in the classpath

I'm trying to run a springboot app on a tomcat server that includes a datasource that's able to communicate with a vault and change db credentials during runtime. The only change I've made in this code is adding some properties that are necessary to communicate with the vault, and changing the datasource configuration to include these vault changes.
I get the following error during startup:
Description:
Cannot determine embedded database driver class for database type NONE
However, in my application.properties file I DO have the driver class specified...
spring.datasource.hikari.driverClassName=com.ibm.db2.jcc.DB2Driver
and in the pom file, I have the correct dependency so the driver is infact included in the classpath... I even see the jar in Intellij's 'External Libraries' drop down.
Again, I've not made many changes besides adding in addtional properties for our vault... and changing the code inside our datasource config to use the vault.
I've compared my changes against another module in which I did the exact same thing, and didn't have this issue at all there.
Does anyone have any ideas as to why this is happening, or suggestions on what I can try?
I've tried including a #Import annotation on my #Configuration class, which points to the vault configuration. I've tried adding a #ComponentScan on my application class to try and really get it to look at the config and properties properly.
If any further detail is required please just let me know. Thanks in advance for any and all help that can be offered.
I have faced same problem and got resolved by below annotation
#SpringDataApplication(exclude = {DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class})
please provide code snippet.So, i can debug it.

Set Spring Boot application.properties based on Environment Variable

I have a Spring Boot application that will run in various environments, and based on the environment it runs in, it will connect to a different database. I have a few application.properties files, one for each environment which look like such:
application-local.properties:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://localhost:5432/mydb
spring.datasource.username=dbuser
spring.datasource.password=123456789
application-someserver.properties:
spring.datasource.platform=postgres
spring.datasource.url=jdbc:postgresql://someserver:5432/myproddb
spring.datasource.username=produser
spring.datasource.password=productionpass
etc. etc.
On each of my environments, I have a environment variable called MYENV which is set to the type of environment it is, for example local or someserver (the name of the application-{env}.properties files perfectly matches the environment name).
How can I get spring boot to read this environment variable and automatically choose the correct .properties file? I don't want to have to do the whole -Dspring.profiles.active=someserver because of the way this package is deployed (it will not be running as a jar).
How can I get spring boot to read this environment variable and
automatically choose the correct .properties file?
Tell Spring Boot to select the Active Profile from the MYENV property or environment variable. One way of doing this is to add the following into your application.properties:
spring.profiles.active=${MYENV}
This way Spring Boot will set spring.profiles.active to the value of MYENV environment variable.
I don't to have to do the whole -Dspring.profiles.active=someserver
because of the way this package is deployed (it will not be running as
a jar)
You can use the corresponding environment variable to that spring.profiles.active, if you don't like to pass a system property through -D arguments. That corresponding environment variable is SPRING_PROFILES_ACTIVE. For example if you set the SPRING_PROFILES_ACTIVE to local, the local profile will be activated. If you're insisting to use MYENV environment variable, the first solution is the way to go.
If you are deploying that application to some container (tomcat, weblogic, ...) you can specify environment variable. For example lets specify environment variable application1.spring.profiles.active=someserver and then in your application.properties set property spring.profiles.active=${application1.spring.profiles.active}
Using Spring context 5.0 I have successfully achieved loading correct property file based on system environment via the following annotation
#PropertySources({
#PropertySource("classpath:application.properties"),
#PropertySource("classpath:application-${MYENV:test}.properties")})
Here MYENV value is read from system environment and if system environment is not present then default test environment property file will be loaded, if I give a wrong MYENV value - it will fail to start the application.
Note: for each profile, you want to maintain you need to make an application-[profile].property file and although I used Spring context 5.0 & not Spring boot - I believe this will also work on Spring 4.1
This is the best solution I believe for my AWS Lambda - with minimal dependencies

Categories