Auto configurations are one of the best features at spring boot. Spring boot supports auto-configured datasource. The credentials for the database are included in Spring boot builds a datasource object dynamically from the configurations. If an error occurs in the initialization phase of the datasource, the error will be thrown.

Normaly, any time this issue has happened, there are two errors seen together. The two errors are shown in the stack trace of exception below. If the configurations are not available, this error will be thrown at the application startup by spring boot.

2020-02-29 08:26:34.599  WARN 47009 --- [           main] ConfigServletWebServerApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'springBootSecurityConfiguration': Unsatisfied dependency expressed through field 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'javax.sql.DataSource' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
2020-02-29 08:26:34.602  INFO 47009 --- [           main] o.apache.catalina.core.StandardService   : Stopping service [Tomcat]
2020-02-29 08:26:34.612  INFO 47009 --- [           main] ConditionEvaluationReportLoggingListener : 

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2020-02-29 08:26:34.680 ERROR 47009 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 



Field dataSource in com.yawintutor.SpringBootSecurityConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)

The following candidates were found but could not be injected:
	- Bean method 'dataSource' in 'JndiDataSourceAutoConfiguration' not loaded because @ConditionalOnProperty (spring.datasource.jndi-name) did not find property 'jndi-name'
	- Bean method 'dataSource' in 'XADataSourceAutoConfiguration' not loaded because @ConditionalOnClass did not find required class 'javax.transaction.TransactionManager'


Consider revisiting the entries above or defining a bean of type 'javax.sql.DataSource' in your configuration.

Root Cause

Spring boot uses the configurations to create database connections. The datasource is a JPA entity, responsible for maintaining the credentials of the database. That helps to create connections to the database.

If in the spring boot application the spring boot JPA module is attached, the datasource is not enabled, then this error occurs.

How to reproduce this issue

Build an application for spring boot with spring boot security module, spring jpa modules and database. This error will occur if the data base details are not configured in the urls of the datasource.

Solution 1

Check the pom.xml file. The “spring-boot-starter-data-jpa” modules and the database should be configured. If this isn’t available, add the dependency modules. For this case it is using the h2 database. So this pom.xml file is connected with the module “com.h2database.”

If you are using a database such as mysql, sqlserver, oracle etc, add the dependency for the respective database in the pom.xml file. The complete pom.xml file is shown as below. 


<?xml version="1.0" encoding="UTF-8"?>
<project xmlns=""
		<relativePath /> <!-- lookup parent from repository -->
	<description>Spring Boot Project</description>









Solution 2

Check the file The url of the data source must be configured in the Spring boot Check the “spring.datasource.url” property to make connections to the database. Make sure that this property is configured correctly in the spring boot application.


Solution 3

Check the custom code, how to build the object Datasource. In some modules, such as spring boot security, it requires adding the datasource to the code. When you skip the datasource then this exception is thrown away. Make sure that the data source object is auto-wired within the spring boot application

package com.yawintutor;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

public class SpringBootSecurityConfiguration extends WebSecurityConfigurerAdapter {
	private DataSource dataSource;
	public void configure(AuthenticationManagerBuilder auth) throws Exception {

	public void configure(HttpSecurity http) throws Exception {

Leave a Reply