Java Can';t@Autowire存储库接口Spring Boot

Java Can';t@Autowire存储库接口Spring Boot,java,spring,spring-mvc,spring-data-jpa,jdbctemplate,Java,Spring,Spring Mvc,Spring Data Jpa,Jdbctemplate,我面临的问题是关于@Autowire存储库接口(在我的例子中是UserRepository),我不知道为什么,但是@Autowire失败了 UserController类调用@服务类,此类调用@组件(DAO类),DAO类是@Autowiring存储库 弹簧靴主开关 package com.leagueofsummoners; import org.springframework.beans.factory.annotation.Autowired; import org.springframe

我面临的问题是关于
@Autowire
存储库接口(在我的例子中是UserRepository),我不知道为什么,但是
@Autowire
失败了

UserController
类调用
@服务
类,此类调用
@组件
(DAO类),DAO类是
@Autowiring
存储库

弹簧靴主开关

package com.leagueofsummoners;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.embedded.ErrorPage;
import org.springframework.boot.orm.jpa.EntityScan;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.http.HttpStatus;

import com.leagueofsummoners.persistence.interfaces.UserRepository;

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

    @Bean
    public EmbeddedServletContainerCustomizer containerCustomizer() {
        return (container -> {
            ErrorPage error401Page = new ErrorPage(HttpStatus.UNAUTHORIZED, "/401.html");
            ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/404.html");
            ErrorPage error500Page = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/500.html");

            container.addErrorPages(error401Page, error404Page, error500Page);
        });
    }
}
DTO类(实体)

存储库界面

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.Repository;

import com.leagueofsummoners.model.dto.UserDTO;

@org.springframework.stereotype.Repository
public interface UserRepository extends Repository<UserDTO, Long> {

    Page<UserDTO> findAll(Pageable pageable);

    UserDTO findByUsernameIgnoringCase(String username);

    UserDTO findByIdUser(int idUser);
}

您需要扫描
jparepositions
在应用程序类中添加此注释:

@EnableJpaRepositories("com.leagueofsummoners.persistence.interfaces")
编辑:

要配置
entityManager
,您需要添加以下依赖项:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

org.springframework.boot
spring引导启动器数据jpa

如果您添加此依赖项,它将自动为您配置存储库,这样您就不需要添加
@EnableJpaRepositories

我尝试过,但尽管失败,但错误似乎有所不同。创建名为“userRepository”的bean时出错:无法创建[org.springframework.orm.jpa.SharedEntityManagerCreator]类型的内部bean(内部bean)#1c6c6f24',而设置bean属性“entityManager”@NoOne自动连线的问题得到解决,这是一个不同的问题guess@NoOne你能添加完整的堆栈吗?@NoOne,这里的问题是,您没有定义entityManager,您可以共享用于spring boot的依赖项吗?不确定您指的是pom.xml,但这里是,但是,是的,我错过了entityManager,我正在检查的指南也错过了它。我猜你会去任何地方查看:·)。非常感谢,伙计。无论谁对我投了反对票,我都想知道他/她为什么这么做:·(不坏的mod只是为了学习Prusposes)x)
@EnableJpaRepositories("com.leagueofsummoners.persistence.interfaces")
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>