【问题标题】:Spring application run failed:UnsatisfiedDependencyExceptionSpring 应用程序运行失败:UnsatisfiedDependencyException
【发布时间】:2019-04-01 06:38:20
【问题描述】:

我是 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

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

【问题讨论】:

  • 您的错误是:Driver com.mysql.jdbc.Driver claims to not accept jdbcUrl, jbdc:mysql://localhost:3306/dbutilisateur。检查您的jdbcUrl
  • 我可以用什么代替?
  • JDBC URL FORMAT: jdbc:mysql://:/,我哪里出错了?
  • 你能添加你的 pom 或 gradle 文件吗
  • 我们需要查看有问题的依赖关系。更广泛地说,除非您有特定理由这样做,否则不要指定driver-class-namehibernate.dialect;让 Boot 处理默认值。

标签: spring hibernate


【解决方案1】:

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

jdbc这个词是错的

【讨论】:

  • 很好。
【解决方案2】:

这里是 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>

【讨论】:

  • 这不是答案;请在您的问题中编辑信息。
猜你喜欢
  • 2021-11-11
  • 1970-01-01
  • 2020-10-26
  • 2019-04-30
  • 2019-08-22
  • 1970-01-01
  • 2020-05-14
  • 1970-01-01
相关资源
最近更新 更多