【问题标题】:Spring doesn't scan packagesSpring不扫描包
【发布时间】:2019-06-14 18:39:44
【问题描述】:

我有一个使用@Repository 注释的存储库

package com.jeppa.interfaces;

import com.jeppa.entities.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface UserRepository extends CrudRepository<User, String> {
    User findByUserEmailIgnoreCase(String useremail);
}

我的控制器的一部分:

package com.jeppa.controllers;

import com.jeppa.entities.ConfirmationToken;
import com.jeppa.entities.User;
import com.jeppa.interfaces.TokenRepository;
import com.jeppa.interfaces.UserRepository;
import com.jeppa.mail.EmailSenderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserAccountController {

    @Autowired
    private UserRepository userRepository;

    @Autowired
    private TokenRepository tokenRepository;

    @Autowired
    private EmailSenderService emailSenderService;


    @RequestMapping(value = "/register", method = RequestMethod.GET)
    public ModelAndView displayRegistration(ModelAndView modelAndView, User user){
        modelAndView.addObject("user", user);
        modelAndView.setViewName("register");
        return modelAndView;
    }

  //////////////     

最后,我的@SpringBootApplication:

package com.jeppa;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

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

我一直收到这个错误

***************************应用程序启动失败


说明:

com.jeppa.controllers.UserAccountController 中的字段 userRepository 需要一个 'com.jeppa.interfaces.UserRepository' 类型的 bean 找不到。

注入点有以下注解: - @org.springframework.beans.factory.annotation.Autowired(required=true)

行动:

考虑定义一个 'com.jeppa.interfaces.UserRepository' 类型的 bean 在您的配置中。

我做错了什么?这是我的项目结构: structure

【问题讨论】:

  • 实体用户没有长ID?您应该在通用接口中使用它而不是 应该是 .
  • 如何将 Spring Data 添加到您的项目中?您使用的是 Spring Boot 启动器(spring-boot-starter-data-jpa)吗?
  • 我的依赖项中的spring-data-jpa
  • pastebin.com/BP6fDs7T 我的依赖项的完整列表

标签: java spring


【解决方案1】:

使用 Spring Boot,您通常应该依赖 Spring Boot 启动器来自动配置您的依赖项。如果是 Spring Data 添加:

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

到您的 pom 文件中自动配置 Spring Data,而不是直接依赖 Spring Data (spring-data-jpa),这需要进一步手动配置。

同时不要忘记添加和配置一个实际的实现(h2、jdbc 等)

【讨论】:

  • 谢谢,我有一点进步,但现在我收到了Related cause: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'userRepository': Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
  • 我认为你没有正确配置你的mysql连接,检查:mkyong.com/spring-boot/…
  • 不,Hibernate: alter table confirmation_token add constraint FKhjrtky9wbd6lbk7mu9tuddqgn foreign key (user_id) references user (user_id) 我可以在我的数据库中看到新表
猜你喜欢
  • 1970-01-01
  • 2016-10-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-10-14
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多