【问题标题】:Can't @Autowire repository interface Spring Boot不能@Autowire 存储库接口 Spring Boot
【发布时间】:2016-07-31 01:11:35
【问题描述】:

我面临的问题是关于 @Autowire 存储库接口(在我的情况下为 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 类(这个在自动装配存储库类时失败)

@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

【问题讨论】:

  • 谁对我投了反对票,我想知道他/她为什么这样做:·)(不错的模式只是为了学习目的)x)

标签: java spring spring-mvc spring-data-jpa jdbctemplate


【解决方案1】:

你需要扫描JpaRepositories在你的应用类中添加这个注解:

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

编辑:

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

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

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

【讨论】:

  • 我试过了,但是它失败了,看起来错误是不同的。创建名为“userRepository”的 bean 时出错:在设置 bean 属性“entityManager”时,无法创建类型为 [org.springframework.orm.jpa.SharedEntityManagerCreator] 的内部 bean '(inner bean)#1c6c6f24'
  • @NoOne 自动连线问题已解决,我猜这是另一个问题
  • @NoOne 你能添加完整的堆栈 strace 吗?
  • @NoOne,这里的问题是你没有定义entityManager,你能分享你用于spring boot的依赖吗?
  • 不确定你是不是说 pom.xml 但这里是... pasted.co/0374ac06 ,但是是的,我缺少 entityManager,我正在检查的指南也错过了它。猜猜会在任何地方检查它:·)。非常感谢老兄。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 1970-01-01
  • 2016-11-19
  • 1970-01-01
  • 2021-11-07
  • 1970-01-01
  • 2019-01-26
相关资源
最近更新 更多