1

I'm trying to create a simple dummy application using Spring Boot Version 1.4.4. Here is how my pom.xml looks like :

<?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.bootstrap</groupId>
<artifactId>bootstrap</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>tsqln</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.4.4.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>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <scope>runtime</scope>
    </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>

My application class looks like:

package com.bootstrap;

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

@SpringBootApplication
public class TsqlnApplication {

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

And my application.properties looks like :

spring.jpa.hibernate.ddl-auto=create
spring.datasource.url=jdbc:mysql://localhost:3306/lostandfound
spring.datasource.username=root
spring.datasource.password=root

But when I run the application I get the following error stating :

*****************************
APPLICATION FAILED TO START
***************************
Description:
Parameter 0 of constructor in org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration required a bean of type 'javax.sql.DataSource' that could not be found.
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
    - Bean method 'dataSource' not loaded because @ConditionalOnClass did not find required class 'org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseType'
Action:
Consider revisiting the conditions above or defining a bean of type 'javax.sql.DataSource' in your configuration.
2017-01-29 17:32:44.645 ERROR 4316 --- [           main] o.s.test.context.TestContextManager      : Caught exception while allowing TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@516be40f] to prepare test instance [com.bootstrap.TsqlnApplicationTests@9573b3b]**

My directory structure for project is :

enter image description here

However with the same approach the above application works with SpringBoot version 1.3. Any suggestions how to make it work with version 1.4.4?

Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
Paras
  • 3,191
  • 6
  • 41
  • 77
  • Possible duplicate of [How to use Spring Boot with MySQL database and JPA?](http://stackoverflow.com/questions/27981789/how-to-use-spring-boot-with-mysql-database-and-jpa) – Yannic Bürgmann Jan 29 '17 at 13:51
  • The reason for my exception is different than the above mentioned question – Paras Jan 29 '17 at 14:25
  • Nevertheless you should be able to solve your problem with the given answer. Maciej Kowalski already provided an answer that should solve your problem, too. – Yannic Bürgmann Jan 29 '17 at 14:48
  • I tried it didn't worked same error. – Paras Jan 30 '17 at 07:26
  • I cloned a demo project from this repo [link](https://github.com/spring-guides/gs-securing-web). The maven run command is executed successfully how ever when I try to run it as a springBoot application I get the same error. – Paras Jan 30 '17 at 07:29
  • What do you mean with. Run it as spring Boot application? Run it inside Eclipse or execute the built jar? Maybe its an issue with eclipse – Yannic Bürgmann Jan 30 '17 at 07:50
  • Yes. I'm using STS and using it's run as option. – Paras Jan 30 '17 at 08:06
  • I can't help you since i dont use sts. But i suggest you add this Detail to your question and adapt the title to something that hints into direction of STS – Yannic Bürgmann Jan 30 '17 at 08:27

3 Answers3

1

Make sure your mysql-connector is actually there in the classpath at runtime (if your using Tomcat, then it should be in the lib folder).

If you do not have it there add a compile dependency in your pom.xml

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

Also you forgot about this line in your application.properties:

spring.datasource.driver-class-name=com.mysql.jdbc.Driver
Maciej Kowalski
  • 25,605
  • 12
  • 54
  • 63
1

in your application.properties add this lines

spring.datasource.url = jdbc:mysql://localhost:3306/yourdatabase
# Username and password
spring.datasource.username = root
spring.datasource.password = 
# Show or not log for each sql query
spring.jpa.show-sql = true
spring.jpa.hibernate.ddl-auto = create
# Naming strategy
spring.jpa.hibernate.naming-strategy = org.hibernate.cfg.ImprovedNamingStrategy
# Allows Hibernate to generate SQL optimized for a particular DBMS
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
e2rabi
  • 4,728
  • 9
  • 42
  • 69
0

You can restore that behaviour by creating your own datasource bean:

@Bean
public DataSource dataSource() {
    return DataSourceBuilder
        .create()
        .username("")
        .password("")
        .url("")
        .driverClassName("")
        .build();
}
Monzurul Shimul
  • 8,132
  • 2
  • 28
  • 42