春季应用程序运行失败:UnsatisfiedDependencyException

时间:2018-10-27 22:12:19

标签: spring hibernate

我是Spring开发的初学者。我试图使用Spring Boot 2.0.6创建一个Web应用程序,但是它给了我以下错误:

Error starting ApplicationContext. To display the conditions report re-run your application with 'debug' enabled.
2018-10-27 22:58:02.226 ERROR 8536 --- [  restartedMain] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactory' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactory' parameter 0; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'entityManagerFactoryBuilder' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Unsatisfied dependency expressed through method 'entityManagerFactoryBuilder' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'jpaVendorAdapter' defined in class path resource [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.orm.jpa.JpaVendorAdapter]: Factory method 'jpaVendorAdapter' threw exception; nested exception is java.lang.RuntimeException: Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jbdc:mysql://localhost:3306/dbutilisateur

这是我的代码:

Utilisateur.java(实体):

package org.sid.entities;
import java.io.Serializable;
import java.util.Date;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
@Entity
public class Utilisateur implements Serializable {
@Id @GeneratedValue
private Long id;
private boolean is_asso;
private String ref_asso;
private String nom;
private String email;
private String adresse;
private String  tel;
private String cin;
private Date dateInscription;
private Date derniereModification;  
public Utilisateur(boolean is_asso, String ref_asso, String nom, String email, String adresse, String tel,
        String cin, Date dateInscription, Date derniereModification) {
    super();
    this.is_asso = is_asso;
    this.ref_asso = ref_asso;
    this.nom = nom;
    this.email = email;
    this.adresse = adresse;
    this.tel = tel;
    this.cin = cin;
    this.dateInscription = dateInscription;
    this.derniereModification = derniereModification;
}
public Utilisateur() {
    super();
    // TODO Auto-generated constructor stub
}
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public boolean isIs_asso() {
    return is_asso;
}
public void setIs_asso(boolean is_asso) {
    this.is_asso = is_asso;
}
public String getRef_asso() {
    return ref_asso;
}
public void setRef_asso(String ref_asso) {
    this.ref_asso = ref_asso;
}
public String getNom() {
    return nom;
}
public void setNom(String nom) {
    this.nom = nom;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public String getAdresse() {
    return adresse;
}
public void setAdresse(String adresse) {
    this.adresse = adresse;
}
public String getTel() {
    return tel;
}
public void setTel(String tel) {
    this.tel = tel;
}
public String getCin() {
    return cin;
}
public void setCin(String cin) {
    this.cin = cin;
}
public Date getDateInscription() {
    return dateInscription;
}
public void setDateInscription(Date dateInscription) {
    this.dateInscription = dateInscription;
}
public Date getDerniereModification() {
    return derniereModification;
}
public void setDerniereModification(Date derniereModification) {
    this.derniereModification = derniereModification;
}   

}

UtilisateurRepository.java

package org.sid.dao;
import org.sid.entities.Utilisateur;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;
@Repository
public interface UtilisateurRepository extends  
JpaRepository<Utilisateur,Long> {
}

DemoApplication.java:

package org.sid;
import org.sid.dao.UtilisateurRepository;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import org.sid.entities.Utilisateur;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired  
private UtilisateurRepository utilisateurRepository;
public static void main(String[] args) {
    SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
    // TODO Auto-generated method stub
    DateFormat df=new SimpleDateFormat("dd/MM/yyyy");       
    utilisateurRepository.save(new Utilisateur(false,"111","oumaima","sfqs@qsfqs","2mars","09876","BK234",df.parse("12/12/2012"),df.parse("30/12/2012")));
    utilisateurRepository.save(new Utilisateur(false,"222","ouma","ouma@qsfqs","3mars","09876","BK234",df.parse("13/12/2012"),df.parse("30/01/2012")));
    utilisateurRepository.findAll().forEach(c->{System.out.println(c.getNom());});}}

和application.properties:

spring.datasource.url=jbdc:mysql://localhost:3306/dbutilisateur
spring.datasource.username=root
spring.datasource.password=
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL5Dialect

有解决方案的建议吗?谢谢

2 个答案:

答案 0 :(得分:1)

在您的spring配置中是jdbc:mysql,而不是jbdc:mysql

单词jdbc是错误的

答案 1 :(得分:0)

这是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>org.sid</groupId>
<artifactId>utilisateurs  </artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

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

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.0.6.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-devtools</artifactId>
        <scope>runtime</scope>
    </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>