簡體   English   中英

@Autowire存儲庫界面無法啟動Spring Boot

[英]Can't @Autowire repository interface Spring Boot

我面臨的問題是關於@Autowire資源庫接口(在我的情況下是UserRepository),我不知道為什么,但是@Autowire失敗了。

UserController類調用@Service類,而該類調用@Component (DAO類),DAO類是@Autowiring @Repository

彈簧啟動主

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(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() {
}

倉庫接口

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類(這在自動裝配庫類時失敗)

@Component
public class UserDAO{

    @Autowired
    private UserRepository userRepository;

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

這是控制台日志的鏈接

您需要掃描JpaRepositories將此注釋添加到您的應用程序類中:

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

編輯:

為了配置entityManager您需要添加以下依賴項:

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

如果添加此依賴項,它將自動為您配置存儲庫,因此您無需添加@EnableJpaRepositories

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM