spring boot autoconfiguration based on env variables - java

In my understanding in a spring-boot application when having spring-boot-starter-data-jpa on your classpath and also mysql-connector
spring-boot will still try to use in memory h2 (as per default), if no db related properties are defined.
But if you have properties like the following:
spring.jpa.hibernate.ddl-auto=update
spring.datasource.url=jdbc:mysql://${MYSQL_HOST:localhost}:3306/db_example
spring.datasource.username=springuser
spring.datasource.password=ThePassword
It will use mysql database.
My question is:
what happens if you have those properties only declared as environmental variables ?
Is this behaviour still valid ?
thanks!

I tried using the following dependencies together: "web", "mysql", "jpa". A simple run without any configuration shows the following error:
***************************
APPLICATION FAILED TO START
***************************
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).
And if you use the environment route instead of "application.properties" as long as you set them correctly it should act the same as a fully configured "application.properties" file.

Related

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.

How to use Eclipse Variables in Spring Boot application.properties

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}

Java : spring-boot-starter-data-jpa dependency in a dependent project

I have build two projects using Java
myproject-db - for db interactions
myproject-api - for exposing api calls
myproject-db uses spring-boot-starter-data-jpa as a dependency and I am able to build and install it all fine.
myproject-api - is a spring boot project and it uses myproject-db as a dependency. When I run my myproject-api project it gives me error
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
I have defined my dataSource url application.properties file in myproject-db like below
spring.datasource.url=jdbc:mysql://localhost:3306/somedb?useSSL=false
spring.datasource.username=root
spring.datasource.password=somepass
application.properties file of myproject-api is empty.
Question is why am I getting this error.
You should have one delivery application configured with Spring Boot with dependent modules
You can have for example
myproject-parent
|
myproject-api
myproject-ui
If your delivery project (main war/jar) is myproject-api then application.properties and #Configuration must be configured in myproject-api and in #SpringBootApplication scope
You cannot have two #SpringBootApplication scopes

Spring boot dependencies error gradle

I have a new spring boot project and I have included some dependencies. The thing is that on the first run the 'rest' and 'jpa' dependencies are working just fine, but on the second run I'm getting a huge error.
dependencies {
compile('org.springframework.boot:spring-boot-starter-cache')
compile("org.springframework.boot:spring-boot-starter-data-rest")
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-hateoas')
compile('org.springframework.boot:spring-boot-starter-mail')
compile('org.springframework.boot:spring-boot-starter-remote-shell')
compile('org.springframework.boot:spring-boot-starter-social-facebook')
compile('org.springframework.boot:spring-boot-starter-social-twitter')
compile('org.springframework.boot:spring-boot-starter-web')
runtime('mysql:mysql-connector-java')
testCompile('org.springframework.boot:spring-boot-starter-test')
}
The error message it the following(it was too big to paste it here):
error msg
I'm using Intelij IDEA 2016.1.1
The important error message from this stack trace seems to be:
Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active)
It says that you didn't define what database to use (what kind of an where it is located).
I guess you need to add some properties to your application.properties file, like:
spring.datasource.url = (URL to your data source)
spring.datasource.driverClassName = (fully qualified class name of your datasource driver)
You could use an H2 in memory database using this:
spring.datasource.url=jdbc:h2:mem:databaseName;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.driverClassName=org.h2.Driver
Note that you also need to include the dependencies for the database into your Gradle dependencies (compile('com.h2database:h2') for an H2).
With java-errors like this you see that there are a lot of lines starting with Caused by:, this is because in the code there are many places where the code catched an exception and then threw it again.
To find the real issue you need to take a look at the last Caused by-entry:
Cannot determine embedded database driver class for database type NONE. If you want an embedded database please put a supported one on the classpath. If you have database settings to be loaded from a particular profile you may need to active it (no profiles are currently active).
I cannot say what the issue itself is with the information you gave. But there are some other threads at stackoverflow that handle this message.

Spring Cloud; CloudFoundry; spring.jpa properties

I am using spring cloud to bind services to a spring boot application deployed to CloudFoundry. When running locally, I can pass Java options to the application as follows:
-Dspring.jpa.hibernate.ddl-auto=create-drop
Now I would like to do the same thing when running the application on CloudFoundry. What's the usual way to do this?
An alternative to setting a system property or environment variable is to set this as a Spring property in src/main/resources/application.properties or src/main/resources/application.yml.
application.properties:
spring.jpa.hibernate.ddl-auto=create-drop
application.yml
spring:
jpa:
hibernate:
ddl-auto: create-drop
With this approach, the configuration will be applied regardless of now the app is deployed - locally, on CF, or on another platform.
You can put an env entry in your manifest.yml file like so:
env:
spring.jpa.hibernate.ddl-auto: create-drop
See more information here:
http://docs.cloudfoundry.org/devguide/deploy-apps/manifest.html#env-block

Categories