2

I am using new version of Spring boot - 1.5.7. But , when I create new spring starter project with jpa dependency, I got strange error:

Caused by: 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: {}

Could anybody help me: Here is my pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.example</groupId> <artifactId>demo-11</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging> <name>demo-11</name> <description>Demo project for Spring Boot</description> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.7.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <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>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 
2
  • post your application.properties file Commented Sep 20, 2017 at 8:27
  • 1
    For the purpose of testing the problem, I create an new spring starter project with jpa dependency, so my application.properties file is empty. But I worked with oracle database otherwise, and when I add oracle dependency it give me the same error. <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> <version>12.1.0.1.0</version> </dependency> Commented Sep 20, 2017 at 8:34

3 Answers 3

2

You need to include a database in your dependencies. If a database was found, spring boot auto-configures your datasource for you.

See this example, which includes a h2database.

https://spring.io/guides/gs/accessing-data-jpa/

Sign up to request clarification or add additional context in comments.

1 Comment

I added oracle dependency <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> <version>12.1.0.1.0</version> </dependency> , but it gives me the same error. I tried alse with mysql : <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency>, but it alse give me the same error
0

You need to put your database related info in the application.properties file

spring.jpa.hibernate.ddl-auto=create spring.datasource.url=jdbc:mysql://localhost:3306/db_example spring.datasource.username=springuser spring.datasource.password=ThePassword 

And you need to put the database dependency in your pom.xml

<dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> </dependency> 

this is how it's listed in the official site for mysql, you can try with your database of choice.

2 Comments

I added oracle dependency <dependency> <groupId>com.oracle</groupId> <artifactId>ojdbc7</artifactId> <version>12.1.0.1.0</version> </dependency> (before that I imported oracle ojdbc7.jar into maven repository). Also, I changed application.properties file: spring.jpa.hibernate.ddl-auto=create spring.datasource.url=jdbc:oracle:thin:@___________:1521/****** spring.datasource.username=******* spring.datasource.password=******* , but it gives me the same error again
if you can post your whole project on git or something i can take a look.
0

You need to place the database in dependencies in the file pom.xml according to the database used in the application.yml file:

  1. application.yml file:

datasource: url: jdbc:postgresql://localhost:5432/your-database username: user password: password

  1. pom.xml file:

<dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <scope>runtime</scope> </dependency>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.