Spring Boot 应用程序在控制器中找不到定义的存储库

Posted

技术标签:

【中文标题】Spring Boot 应用程序在控制器中找不到定义的存储库【英文标题】:SpringBoot Application cannot find defined Repository in Controller 【发布时间】:2018-04-02 12:55:45 【问题描述】:

我按照本指南将 mysql 添加到已存在的 SpringBoot 项目中,该项目的依赖项管理位于 graddle 上。就在我添加教程中使用的这三个类时,如下所示

main/java/net/code/model/Users.Java

package net.code.controller;
import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController   // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController 
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(path="/add") // Map ONLY GET Requests
    public @ResponseBody String addNewUser (@RequestParam String name
            , @RequestParam String email) 
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        User n = new User();
        n.setName(name);
        n.setEmail(email);
        userRepository.save(n);
        return "Saved";
    

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() 
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    

和用户存储库为 主/java/net/code/repo/UserRepository.Java 打包 net.code.repo;

import net.code.model.User;
import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

// This will be AUTO IMPLEMENTED by Spring into a Bean called userRepository
// CRUD refers Create, Read, Update, Delete

@Repository
public interface UserRepository extends CrudRepository<User, Long> 


带有网络服务控制器 main/java/net/code/controller/MainController.Java

package net.code.controller;

import net.code.model.User;
import net.code.repo.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@RestController   // This means that this class is a Controller
@RequestMapping(path="/demo") // This means URL's start with /demo (after Application path)
public class MainController 
    @Autowired // This means to get the bean called userRepository
    // Which is auto-generated by Spring, we will use it to handle the data
    private UserRepository userRepository;

    @GetMapping(path="/add") // Map ONLY GET Requests
    public @ResponseBody String addNewUser (@RequestParam String name
            , @RequestParam String email) 
        // @ResponseBody means the returned String is the response, not a view name
        // @RequestParam means it is a parameter from the GET or POST request

        User n = new User();
        n.setName(name);
        n.setEmail(email);
        userRepository.save(n);
        return "Saved";
    

    @GetMapping(path="/all")
    public @ResponseBody Iterable<User> getAllUsers() 
        // This returns a JSON or XML with the users
        return userRepository.findAll();
    

@SpringBoot 我的班级 main/java/net/code/App.Java

package net.code;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

//@CrossOrigin(origins = "http://127.0.0.1:8080")

@SpringBootApplication

@ComponentScan("net.code")
//@ComponentScan(basePackages =  "net.code","net.code.repo")

//@RestController
@EnableAutoConfiguration(exclude=DataSourceAutoConfiguration.class)
public class App extends WebMvcConfigurerAdapter 

    public static void main(String[] args) 
        SpringApplication.run(App.class, args);
    


但每当我运行应用程序时,我都会不断收到以下消息

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled.
2017-10-21 15:11:59.674 ERROR 67424 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

Field userRepository in net.code.controller.MainController required a bean of type 'net.code.repo.UserRepository' that could not be found.


Action:

Consider defining a bean of type 'net.code.repo.UserRepository' in your configuration.


Process finished with exit code 1

我正在搜索类似的相关问题 Spring Boot not autowiring @Repository, @RestController in other package doesn't work 但无法修复,因为这些链接的建议对我不起作用 我也想在这里尝试接受的解决方案 Consider defining a bean of type 'package' in your configuration [Spring-Boot] 但我发现@EnableJpaRepositories 没有这样的包

请帮助我解决这个问题,因为我这几天一直在尝试解决这个问题

【问题讨论】:

试试@EnableJpaRepositories(basePackages="&lt;repository package name&gt;") 你一定是做错了什么。同样的代码在我的系统上运行良好 我知道有问题,但有问题吗?但是,@EnableJpaRepositories 不会在我的 IDE 中编译,就像丢失的包一样,但我不知道如何获取 @olyjosh,在这里查看完整的源代码github.com/olantobi/spring-boot-repository 请注意 SpringBootApplication 是一个负载很重的注解,旨在替换您上面的其他注解 - docs.spring.io/spring-boot/docs/current/reference/html/…。这意味着您在 DataSourceAutoConfiguration 上被排除在外,例如可能会被忽略。您应该考虑将您的排除和组件扫描捆绑到 @SpringBootApplication 本身,请参阅 api 文档了解更多信息 - docs.spring.io/spring-boot/docs/current/api/org/springframework/… 【参考方案1】:

此代码的完美工作版本是here on github。可以查看http://start.spring.io获取spring-data-jpa对应的gradle版本。

【讨论】:

以上是关于Spring Boot 应用程序在控制器中找不到定义的存储库的主要内容,如果未能解决你的问题,请参考以下文章

Spring在基本的Spring Boot应用程序中找不到bean [重复]

在简单的 Maven 项目中找不到依赖 spring-boot-starter-security

Spring Boot + Spring Security + Tomcat = 在应用程序上下文中找不到可见的 WebSecurityExpressionHandler 实例

Spring Boot 2.0.0,在自动配置 jar 中找不到 DataSourceBuilder

Spring boot graphql 在类路径中找不到 graphql 模式文件

Spring Boot jdbc模板在自定义模式下的postgres中找不到表