gpt4 book ai didi

java - 无法@Autowire存储库接口(interface)Spring Boot

转载 作者:行者123 更新时间:2023-12-03 02:56:23 26 4
gpt4 key购买 nike

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

UserController 类调用 @Service 类,该类调用 @Component (DAO 类),DAO 类是 @Autowiring @Repository

Spring启动主

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 类(实体)

@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 类( Autowiring 存储库类时失败)

@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

最佳答案

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

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

编辑:

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

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

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

关于java - 无法@Autowire存储库接口(interface)Spring Boot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36522174/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com