为了账号安全,请及时绑定邮箱和手机立即绑定

BeanCreationException:创建名为“userRepository”

BeanCreationException:创建名为“userRepository”

回首忆惘然 2022-01-06 19:58:49
我尝试实现这个用户管理模板并使其适应我的需要,但 Springboot 应用程序不再启动,我遇到了这个错误:org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'securityConfig': Unsatisfied dependency expressed through field 'userService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userServiceImpl': Unsatisfied dependency expressed through field 'userRepository'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Post-processing of merged bean definition failed; nested exception is java.lang.NoClassDefFoundError: javax/persistence/SynchronizationType我无法弄清楚出了什么问题。请帮忙?
查看完整描述

3 回答

?
拉莫斯之舞

TA贡献1820条经验 获得超10个赞

我认为您必须从 pom.xml


<dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-core</artifactId>

    <version>4.1.4.Final</version>

</dependency>

Spring Boot 2.1已经有Hibernate依赖项而且它使用Hibernare v.5所以这里可能有一些问题Hibernate v.4而且我没有在你正在关注的教程中找到这样的依赖项


UPD 例外情况


org.springframework.beans.factory.BeanCreationException:创建名为“entityManagerFactory”的 bean 在类路径资源 [org/springframework/boot/autoconfigure/orm/jpa/HibernateJpaConfiguration.class] 中定义时出错:调用 init 方法失败;嵌套异常是 javax.persistence.PersistenceException: [PersistenceUnit: default] Unable to build Hibernate SessionFactory; 嵌套异常是 org.hibernate.cfg.beanvalidation.IntegrationException:激活 Bean 验证集成时出错


阅读这个( jaxb-api的最新版本是 2.3.1)。作为一种选择,我还建议您尝试将 a 添加@EnableTransactionManagement到配置类


UPD2


我终于设法重现了异常(


...激活 Bean 验证集成时出错


) 在我的环境中并找出问题的根源。由于您已经添加了此依赖项:


<dependency>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-validation</artifactId>

</dependency>

您不应添加任何其他验证依赖项。所以要摆脱这个问题,你必须删除这个:


<dependency>

    <groupId>javax.validation</groupId>

    <artifactId>validation-api</artifactId>

    <version>1.1.0.Final</version>

</dependency>


查看完整回答
反对 回复 2022-01-06
?
精慕HU

TA贡献1845条经验 获得超8个赞

您问题的根本原因是不正确的依赖关系。您可以从 pom.xml 中注释掉以下依赖项:


<dependency>

    <groupId>javax.validation</groupId>

    <artifactId>validation-api</artifactId>

    <version>1.1.0.Final</version>

</dependency>

<dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-validator</artifactId>

    <version>5.4.1.Final</version>

</dependency>

<dependency>

    <groupId>org.hibernate</groupId>

    <artifactId>hibernate-core</artifactId>

    <version>4.1.4.Final</version>

</dependency>

接下来你可以mvn dependency:tree在你的项目上做一个,并看到它org.springframework.boot:spring-boot-starter-web:jar:2.1.0.RELEASE传递了这两个组件的正确版本:


org.hibernate.validator:hibernate-validator:jar:6.0.13.Final

javax.validation:validation-api:jar:2.0.1.Final

org.springframework.boot:spring-boot-starter-data-jpa 传递性拉入:


org.hibernate:hibernate-core:jar:5.3.7.Final

数据库上的存储库操作在此设置下可以正常工作。


pom.xml


<parent>

    <groupId>org.springframework.boot</groupId>

    <artifactId>spring-boot-starter-parent</artifactId>

    <version>2.1.0.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-web</artifactId>

    </dependency>


    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-devtools</artifactId>

        <scope>runtime</scope>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-thymeleaf</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-tomcat</artifactId>

        <scope>provided</scope>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-data-jpa</artifactId>

    </dependency>

    <dependency>

        <groupId>com.h2database</groupId>

        <artifactId>h2</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-data-rest</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-security</artifactId>

    </dependency>

    <dependency>

        <groupId>org.thymeleaf.extras</groupId>

        <artifactId>thymeleaf-extras-springsecurity4</artifactId>

        <version>3.0.1.RELEASE</version>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.security</groupId>

        <artifactId>spring-security-taglibs</artifactId>

    </dependency>

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-mail</artifactId>

    </dependency>

    <dependency>

        <groupId>commons-beanutils</groupId>

        <artifactId>commons-beanutils</artifactId>

    </dependency>


    <!-- bootstrap and jquery -->

    <dependency>

        <groupId>org.webjars</groupId>

        <artifactId>bootstrap</artifactId>

        <version>3.3.7</version>

    </dependency>

    <dependency>

        <groupId>org.webjars</groupId>

        <artifactId>jquery</artifactId>

        <version>3.2.1</version>

    </dependency>


    <!-- mysql connector -->

    <dependency>

        <groupId>mysql</groupId>

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

        <scope>runtime</scope>

    </dependency>

    <dependency>

        <groupId>com.icegreen</groupId>

        <artifactId>greenmail</artifactId>

        <version>1.5.5</version>

        <scope>test</scope>

    </dependency>

    <!--<dependency>-->

        <!--<groupId>javax.validation</groupId>-->

        <!--<artifactId>validation-api</artifactId>-->

        <!--<version>1.1.0.Final</version>-->

    <!--</dependency>-->

    <!--<dependency>-->

        <!--<groupId>org.hibernate</groupId>-->

        <!--<artifactId>hibernate-validator</artifactId>-->

        <!--<version>5.4.1.Final</version>-->

    <!--</dependency>-->

    <dependency>

        <groupId>org.springframework.boot</groupId>

        <artifactId>spring-boot-starter-validation</artifactId>

    </dependency>

    <dependency>

        <groupId>commons-codec</groupId>

        <artifactId>commons-codec</artifactId>

        <version>1.10</version>

    </dependency>

    <!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->

    <dependency>

        <groupId>commons-beanutils</groupId>

        <artifactId>commons-beanutils</artifactId>

        <version>1.9.3</version>

    </dependency>

    <!--<dependency>-->

        <!--<groupId>org.hibernate</groupId>-->

        <!--<artifactId>hibernate-core</artifactId>-->

        <!--<version>4.1.4.Final</version>-->

    <!--</dependency>-->

</dependencies>

依赖树


[INFO] +- org.springframework.boot:spring-boot-starter-web:jar:2.1.0.RELEASE:compile

[INFO] |  +- org.hibernate.validator:hibernate-validator:jar:6.0.13.Final:compile

[INFO] |  |  +- javax.validation:validation-api:jar:2.0.1.Final:compile

[INFO] |  |  +- org.jboss.logging:jboss-logging:jar:3.3.2.Final:compile

[INFO] |  |  \- com.fasterxml:classmate:jar:1.4.0:compile


查看完整回答
反对 回复 2022-01-06
?
慕森王

TA贡献1777条经验 获得超3个赞

有人告诉我问题是我需要一个实际的邮件服务器一起运行。我不知道。对您来说,这听起来像是这次崩溃的合理原因吗?有关如何设置电子邮件服务器并将其链接到 Maven 项目的优秀教程的任何链接?我找不到任何。


查看完整回答
反对 回复 2022-01-06
  • 3 回答
  • 0 关注
  • 223 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信