Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 376 Vote(s) - 3.55 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Spring Boot - Error creating bean with name 'dataSource' defined in class path resource

#11
In my case I just ignored the following in application.properties file:

\# Hibernate

\#spring.jpa.hibernate.ddl-auto=update

It works for me....
Reply

#12
Check that you have database dependency at **runtime** group at build.gradle

runtime group: 'com.h2database', name: 'h2', version: '1.4.194'

or change scope from test to **runtime** if you use Maven

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.194</version>
<scope>runtime</scope>
</dependency>
Reply

#13
Give you something different, when you encounter this kind of error, cannot create
bean datasource in a test case.

It might be caused by some reasons:

1. No datasource, you will need to create your datasource, h2 in-memory datasource or whatever, or you can choose the way like `exclude={datasource··}`.
2. You have your datasource, like MySQL, but it still not work. It was caused by class `AutoConfigureTestDatabase`, It will choose a datasource for you which may cause ambiguity.

Solution: add `@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE)` to avoid replace the default datasource.
Reply

#14
This problem comes while you are running Test.
Add dependency

testCompile group: 'com.h2database', name: 'h2', version: '1.4.197'
Add folder resources under test source add file bootstrap.yml
and provide content.

spring:
datasource:
type: com.zaxxer.hikari.HikariDataSource
url: jdbc:h2:mem:TEST
driver-class-name: org.h2.Driver
username: username
password: password
hikari:
idle-timeout: 10000

this will setup your data source.
Reply

#15
I was facing the same problem for several days, and finally the issue isn't with the code, the problem commes from maven, you must delete all the files that he downloaded from your hard drive "C:\Users\username\.m2\repository", and do another update maven for your project, that will fix your problem.
Reply

#16
I solved my problem with the change of the parent Spring Boot Dependency.

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.0.RELEASE</version>
</parent>

to

<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.6.RELEASE</version>
</parent>

For more Information, take a look at the release notes: [Spring Boot 2.1.0 Release Notes][1]


[1]:

[To see links please register here]

Reply

#17
I was getting the same error, found out it was due to some of the dependencies missing in my pom.xml like that of Spring JPA, Hibernate, Mysql or maybe Jackson.
So make sure that dependencies are not missing in your pom.xml and check their version compatibility.

<!-- Jpa and hibernate -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-orm</artifactId>
<version>4.2.2.RELEASE</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.0.3.Final</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>

Reply

#18
Not directly related to the original question but this will be useful for someone. This error occurred to me with a simple two project structure. One project was handling some database operations with spring JDBC (say A) and the other did not have any JDBC operations at all(say B). But still, this error appeared while I was starting service B. Saying the datasource should be initialized properly.

As I figured out I had added this dependency to the parent pom of the two modules

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>

This caused spring to initialize the JDBC dependencies for project B too. So, I moved it to project A's pom, everything was fine.

Hope this would help someone
Reply

#19
**Created database in MySQL**

create database springboot2;

**application.properties**

spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.datasource.url = jdbc:mysql://localhost:3306/springboot2
spring.datasource.username = root
spring.datasource.password = root
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = update
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
server.port=9192

**pom.xml**

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>org.junit.vintage</groupId>
<artifactId>junit-vintage-engine</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
**main class**

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}

}

**model class**

package com.First.Try.springboot.entity;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Table;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@AllArgsConstructor
@NoArgsConstructor
@Entity
@Table(name="PRODUCT_TBL1")
public class Product {
@Id
@GeneratedValue
private int id;
private String name;
private int quantity;
private double price;
....
....
....
}
Reply

#20
By default, with the latest version of Spring Boot, the load of data.sql is done before the tables are created. So use - spring.jpa.defer-datasource-initialization=true

Example -


**In application.properties :- **
spring.jpa.show-sql=true
spring.datasource.url=jdbc:h2:mem:testdb
spring.h2.console.enabled=true
spring.jpa.defer-datasource-initialization=true

Thanks
Athar Karim
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through