Spring Boot 似乎没有找到 Repository

Posted

技术标签:

【中文标题】Spring Boot 似乎没有找到 Repository【英文标题】:Spring Boot doesn't seem to find Repository 【发布时间】:2017-11-13 08:11:50 【问题描述】:

我不确定我的配置有什么问题,但据我所知,它应该是正确的,尽管 spring 宣布没有这样的 bean 定义。我非常认同以下几点:

配置类在其他所有东西的根包中 存储库扩展了 JpaRepository,因此不需要注释 播放器对象带有“@Entity”注解 此存储库的 pojo 完全根据注释映射 不存在非 java 配置文件 数据库已设置并使用正确的凭据。 出于开发目的,Jpa/hibernate 设置为“Create-Update”。

这是我的完整调试日志: spring startup log pastebin

不带调试标志的日志:

2017-06-12 09:34:36.395  INFO 12880 --- [  restartedMain] 
d.a.c.newworlds.NewWorldsApplication     : No active profile set, falling back to default profiles: default
2017-06-12 09:34:36.727  INFO 12880 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@10057b9b: startup date [Mon Jun 12 09:34:36 CEST 2017]; root of context hierarchy
2017-06-12 09:34:38.090  INFO 12880 --- [  restartedMain] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'playerRepository' with a different definition: replacing [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2017-06-12 09:34:38.107  INFO 12880 --- [  restartedMain] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'userAccountRepository' with a different definition: replacing [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2017-06-12 09:34:38.115  INFO 12880 --- [  restartedMain] o.s.b.f.s.DefaultListableBeanFactory     : Overriding bean definition for bean 'organizationRepository' with a different definition: replacing [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null] with [Root bean: class [org.springframework.data.jpa.repository.support.JpaRepositoryFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
2017-06-12 09:34:38.612  INFO 12880 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration' of type [class org.springframework.boot.autoconfigure.validation.ValidationAutoConfiguration] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-06-12 09:34:38.664  INFO 12880 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'validator' of type [class org.springframework.validation.beanvalidation.LocalValidatorFactoryBean] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-06-12 09:34:38.701  INFO 12880 --- [  restartedMain] trationDelegate$BeanPostProcessorChecker : Bean 'org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration' of type [class org.springframework.transaction.annotation.ProxyTransactionManagementConfiguration$$EnhancerBySpringCGLIB$$93eeb181] is not eligible for getting processed by all BeanPostProcessors (for example: not eligible for auto-proxying)
2017-06-12 09:34:39.009  INFO 12880 --- [  restartedMain] s.b.c.e.t.TomcatEmbeddedServletContainer : Tomcat initialized with port(s): 8080 (http)
2017-06-12 09:34:39.019  INFO 12880 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service Tomcat
2017-06-12 09:34:39.020  INFO 12880 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/8.5.11
2017-06-12 09:34:39.243  INFO 12880 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2017-06-12 09:34:39.243  INFO 12880 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 2521 ms
2017-06-12 09:34:39.635  INFO 12880 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-06-12 09:34:39.635  INFO 12880 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-06-12 09:34:39.636  INFO 12880 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-06-12 09:34:39.636  INFO 12880 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-06-12 09:34:39.637  INFO 12880 --- [ost-startStop-1] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2017-06-12 09:34:39.637  INFO 12880 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2017-06-12 09:34:40.130  INFO 12880 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Building JPA container EntityManagerFactory for persistence unit 'default'
2017-06-12 09:34:40.156  INFO 12880 --- [  restartedMain] o.hibernate.jpa.internal.util.LogHelper  : HHH000204: Processing PersistenceUnitInfo [
name: default
...]
2017-06-12 09:34:40.266  INFO 12880 --- [  restartedMain] org.hibernate.Version                    : HHH000412: Hibernate Core 5.0.11.Final
2017-06-12 09:34:40.267  INFO 12880 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000206: hibernate.properties not found
2017-06-12 09:34:40.271  INFO 12880 --- [  restartedMain] org.hibernate.cfg.Environment            : HHH000021: Bytecode provider name : javassist
2017-06-12 09:34:40.324  INFO 12880 --- [  restartedMain] o.hibernate.annotations.common.Version   : HCANN000001: Hibernate Commons Annotations 5.0.1.Final
2017-06-12 09:34:40.890  INFO 12880 --- [  restartedMain] org.hibernate.dialect.Dialect            : HHH000400: Using dialect: org.hibernate.dialect.mysqlDialect
2017-06-12 09:34:41.031  WARN 12880 --- [  restartedMain] org.hibernate.cfg.AnnotationBinder       : HHH000138: Mixing inheritance strategy in a entity hierarchy is not allowed, ignoring sub strategy in: de.awesome.corporate.newworlds.core.planets.entity.AbstractPlanetoid
2017-06-12 09:34:41.186  WARN 12880 --- [  restartedMain] o.h.b.i.SessionFactoryBuilderImpl        : Unrecognized hbm2ddl_auto value : create-update.  Supported values include create, create-drop, update, and validate.  Ignoring
2017-06-12 09:34:41.604  INFO 12880 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Initialized JPA EntityManagerFactory for persistence unit 'default'
2017-06-12 09:34:41.930  WARN 12880 --- [  restartedMain] ationConfigEmbeddedWebApplicationContext : Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'playerRegistrationController': Unsatisfied dependency expressed through field 'registrationService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'registrationService': Unsatisfied dependency expressed through field 'playerRepository'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'de.awesome.corporate.newworlds.core.usermanagement.repository.PlayerRepository' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: @org.springframework.beans.factory.annotation.Autowired(required=true)
2017-06-12 09:34:41.931  INFO 12880 --- [  restartedMain] j.LocalContainerEntityManagerFactoryBean : Closing JPA EntityManagerFactory for persistence unit 'default'
2017-06-12 09:34:41.944  INFO 12880 --- [  restartedMain] utoConfigurationReportLoggingInitializer : 

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-06-12 09:34:42.090 ERROR 12880 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field playerRepository in de.awesome.corporate.newworlds.core.usermanagement.service.RegistrationService required a bean of type 'de.awesome.corporate.newworlds.core.usermanagement.repository.PlayerRepository' that could not be found.


Action:

Consider defining a bean of type 'de.awesome.corporate.newworlds.core.usermanagement.repository.PlayerRepository' in your configuration.

我的 SpringBootApplication:

package de.awesome.corporate.newworlds;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
public class NewWorldsApplication 

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

我的 PlayerRepository:

package de.awesome.corporate.newworlds.core.usermanagement.repository;

import org.springframework.data.jpa.repository.JpaRepository;

import de.awesome.corporate.newworlds.core.usermanagement.entity.Player;

public interface PlayerRepository extends JpaRepository<Player, String>

    Player findByUserAccount(String useraccount);


调用服务:

package de.awesome.corporate.newworlds.core.usermanagement.service;
@Service
public class RegistrationService 

    @Autowired
    private PlayerRepository playerRepository;

    @Autowired
    private UserAccountRepository userRepository;

    @Autowired
    private OrganizationCreationService organizationCreationService;

    @Autowired
    private BCryptPasswordEncoder passwordEncoder;

POM:

<?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>de.awesome.corporate.newworlds</groupId>
<artifactId>New-Worlds</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>new-worlds</name>
<description>Text-based Management Game</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.1.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-aop</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-hateoas</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.session</groupId>
        <artifactId>spring-session</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</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>
    </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-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

编辑:似乎从 pom 中删除 spring-data-jpa-starter 然后重新插入后,它现在自动装配密码编码器也有问题。 spring 自动装配的方式是否存在普遍问题?

【问题讨论】:

在问题本身中粘贴日志、POM 等。 现在,为什么我的问题被否决了? 请不要让人们点击链接以获得完整的图片。将其添加到您的问题中。我也怀疑你所说的是你的 pom 是你实际的 pom 文件。请添加您的实际 pom 文件,而不是有效 pom.xml。 @M.Deinum 我已经处理了您的反馈。但是,我无法在启用 spring 调试的情况下发布整个日志,因为它超出了字符限制。 @harshavmb for JpaRepositories 您可以通过方法名称中的特定语法定义查询,看这里docs.spring.io/spring-data/jpa/docs/current/reference/html/… 【参考方案1】:

显然,在项目结构的某个地方,有不同版本的存储库和不同版本的播放器。

虽然通常我希望 Spring 会产生一个错误,告诉我有多个具有相同类型描述的服务,但我怀疑由于 player 和 playerRepository 都是重复的,这种机制没有产生预期的错误方法,而是被告知我说根本没有该名称的服务。

【讨论】:

【参考方案2】:

我的问题类似:我有多个 ORM 类和多个用于同一实体的存储库,这导致应用程序无法正常运行。

【讨论】:

以上是关于Spring Boot 似乎没有找到 Repository的主要内容,如果未能解决你的问题,请参考以下文章

Spring Boot with docker 无法找到请求目标错误的有效认证路径

Spring Boot:@Entity 注解是不是存在?

Spring Boot JpaRepository 保存调用似乎没有做任何事情

Spring Boot 和 FreeMarker

Gradle 找不到集成测试、JUnit 5 和 Spring Boot:没有找到给定的测试包括:

Spring Boot没有连接到RabbitMQ