java 不能@Autowire存储库接口Spring Boot

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36522174/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 01:33:41  来源:igfitidea点击:

Can't @Autowire repository interface Spring Boot

javaspringspring-mvcspring-data-jpajdbctemplate

提问by No One

The problem I'm facing is about @Autowirethe repository interface (UserRepository in my case), I don't know why, but the @Autowireis failing.

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

UserControllerclass calls a @Serviceclass and this one calls a @Component(DAO class), the DAO class is @Autowiringthe @Repository.

UserController类调用一个@Service类而这一次调用@Component(DAO类),在DAO类是@Autowiring所述@Repository

Spring boot main

弹簧靴主要

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 CLASS (Entity)

DTO 类(实体)

@Entity(name = "user")
@Table(name = "users")
public class UserDTO implements Serializable {

private static final long serialVersionUID = 1L;

@Id
@Column(name = "id_user")
private Long idUser;

@Column(nullable = false, name = "summoner_name")
private String summonerName;
@Column(nullable = false)
private String username;
@Column(nullable = false)
private String password;
@Column(nullable = false)
private String email;
@Column(nullable = false)
private String avatar;
@Column(nullable = false)
private String firma;
@Column(nullable = false, name = "permission_level")
private PermissionLevels permissionLevel;

public UserDTO() {
}

Repository interface

存储库接口

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);
}

DAO class (this one failing when autowiring repository class)

DAO 类(这个在自动装配存储库类时失败)

@Component
public class UserDAO{

    @Autowired
    private UserRepository userRepository;

    public UserDTO findByUsernameIgnoringCase(String username) {
        return this.userRepository.findByUsernameIgnoringCase(username);
    }
}

Here's a link with the log of the console

这是控制台日志的链接

回答by Rafik BELDI

You need to scan for JpaRepositoriesadd this annotation on your application Class :

您需要扫描以JpaRepositories在您的应用程序 Class 上添加此注释:

@EnableJpaRepositories("com.leagueofsummoners.persistence.interfaces")

EDIT:

编辑:

In order to configure the entityManageryou need to add the following dependency :

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

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

if you add this dependency it will configure the repositories automatically for you so you don't need to add the@EnableJpaRepositories.

如果您添加此依赖项,它将自动为您配置存储库,因此您无需添加@EnableJpaRepositories.