SpringBoot DataSource configuration - spring

I'm trying to use the application.properties file to configure the datasource Spring Boot will have to use.
I've put the following properties inside :
spring.datasource.driverClassName=org.postgresql.Driver
spring.datasource.user=test
spring.datasource.password=test
spring.datasource.jdbcUrl=jdbc:postgresql://localhost:5432/test
The application.properties file is well used by other systems. But I can't get it to work for the automatic datasource configuration.
I'm still getting this Exception :
org.springframework.beans.factory.BeanCreationException: Cannot determine embedded database url for database type NONE.
The postgresql driver is included and loaded. And I can configure the datasource using a Configuration class, and the same parameters as above.
I've also added the #EnableAutoConfiguration and #EnableJpaRepositories to my Application.class.
Any clues?

You should use spring.datasource.url to configure the JDBC URL rather than spring.datasource.jdbcUrl.
spring.datasource.jdbcUrl will work if the specific DataSource implementation that you're using has a setJdbcUrl method (HikariCP, for example) where as spring.datasource.url will work with any of the supported datasources.
Using spring.datasource.url also has the added benefit that you don't need to specify spring.datasource.driverClassName as it will be inferred from the url.

Related

When I define a datasource ,what's the different between server.xml datesource and application.properties

My application runs on liberty application server, using springboot .And I define a datasource in server.xml file , and also I define the datasource in application.properties.
What's the different between the two file?

How to access defined in the application.properties file in Spring Boot?

I want to access values provided in application.properties, e.g.:
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
logging.file=${HOME}/application.log
userBucket.path=${HOME}/bucket
I want to access userBucket.path in my main program in a Spring Boot application
You can use the #Value annotation and access the property in whichever Spring bean you're using
#Value("${userBucket.path}")
private String userBucketPath;

Set a Spring Boot property using a value from JNDI

How can I put a string stored in JNDI to Spring Boot properties?
A bit more details:
I get my DataSource from JNDI using this property in application.properties:
spring.datasource.jndi-name=my_data_source_jndi_name. Currently, the DB schema is hard coded in application.properties in the following way: spring.jpa.properties.hibernate.default_schema=my_schema. There is a JNDI record that containd the DB schema name. How can I get the schema from JNDI too?
As outlined here:
https://docs.spring.io/spring-boot/docs/current/reference/html/spring-boot-features.html#boot-features-external-config
Spring will resolve properties from JNDI and so you can therefore parameterise it in various ways. You can bypass the properties file and use the #Value() annotation directly in Java config
#Value("my.property.in.jndi")
private String schema;
or you can do as below in the properties file:
spring.jpa.properties.hibernate.default_schema=${my.property.in.jndi}

Spring boot provided "spring.datasource.password" can change to "spring.ds.pwd" without creating new bean for DataSource

My team is using latest spring boot for one of the project.
Spring provides database configuration in application.properties file and for password this is the key "spring.datasource.password"
Now, we want to change this to "spring.ds.pwd", but, the change should not required to create new Bean for datasource is it possible?
I followed below link, but, here new beans are creating to handle the session, which should not be expected. Other search information is also similar.
https://dzone.com/articles/spring-boot-jpa-mysql-sample-app-code-example
I was able to achieve what I want by overriding the DataSourceProperties java class with same package name.

Using #ConfigurationProperties in Spring Boot Application doesn't work

I am using Spring Boot V 1.4.1 for a new application.
My app requires two JDBC data sources and I was following the example at http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-two-datasources how to set it up.
My Spring beans configuration class is annotated with #EnableConfigurationProperties and my first bean is defined as
#Primary
#Bean
#ConfigurationProperties(prefix = "first.database")
DataSource qivsDB() {
return DataSourceBuilder.create().build();
}
, the second one accordingly. My application.properties file has properties defined like
first.database.url=jdbc:[redacted]
first.database.username=[redacted]
first.database.password=[redacted]
For reasons I not transparent to me during debugging this is failing to initialize: Cannot determine embedded database driver class for database type NONE - debug showed me that the builder does not have any properties set when calling build().
What did I miss here?
Before you do all the debugging part, you should have a look to the auto-configuration report. If you define your own DataSource there's no reason for Spring Boot to start looking at what it can do for your app. So, for some reasons, that definition of yours is not applied in your app and the default in Spring Boot still applies, doesn't find any JDBC url in the default namespace and attempt to start an embedded database. You should see in the auto-config report that the DataSourceAutoConfiguration still matches.
I am not sure the public keyword has anything to do with it, though you won't get custom meta-data for that key since we only scan for public methods.

Resources