简体   繁体   English

SpringBoot应用程序无法在Controller中找到已定义的存储库

[英]SpringBoot Application cannot find defined Repository in Controller

I was following this guide to add MySql to an already existing SpringBoot project whose dependency management is on graddle. 我正在按照本指南将MySql添加到已存在的SpringBoot项目中,该项目的依赖项管理处于缓慢状态。 Just when I added these three class used in the tutorial as follow 就在我添加本教程中使用的这三个类时,如下所示

main/java/net/code/model/Users.Java 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();
    }
}

and a user repository as main/java/net/code/repo/UserRepository.Java package net.code.repo; 用户存储库为main / 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> {

}

with a webservice controller main/java/net/code/controller/MainController.Java 使用Web服务控制器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();
    }
}

My class with @SpringBoot main/java/net/code/App.Java 我的@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);
    }

}

But any time I run the application, I keep getting the below message 但是无论何时运行该应用程序,我都会不断收到以下消息

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

I have search for relevant issue like these Spring Boot not autowiring @Repository , @RestController in other package doesn't work but couldn't fix as suggestion from those link didn't work for me I also wanted to try the accepted solution here Consider defining a bean of type 'package' in your configuration [Spring-Boot] but I find out that there is no such package for @EnableJpaRepositories 我寻找像这些相关的问题春天启动不自动装配@Repository@RestController在其他包不能正常工作 ,但无法修复的那些链接的建议,我没有工作,我也想尝试接受的解决方案在这里考虑在您的配置中定义了“包”类型的bean [Spring-Boot],但是我发现@EnableJpaRepositories没有这样的包

Please help me out on this as I have been trying to fix this for days now 请帮我解决这个问题,因为我已经尝试了好几天了

A perfectly working version of this code is here on github . 这段代码的完美工作版本在github上 You can check http://start.spring.io to get the corresponding gradle version for spring-data-jpa. 您可以检查http://start.spring.io以获得spring-data-jpa的相应gradle版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM