当前位置: 首页 > 知识库问答 >
问题:

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

秦珂
2023-03-14

我正在按照本指南将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();
    }
}
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> {

}
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没有自动连接@Repository,@RestController在其他包中不起作用但无法修复,因为那些链接中的建议对我不起作用。我还想尝试一下这里被接受的解决方案考虑在配置[Spring-Boot]中定义一个类型为'package'的bean,但我发现@EnableJParepositories没有这样的包

请帮我解决这个问题,因为我已经试着解决这个问题好几天了

共有1个答案

步致远
2023-03-14

GitHub上有一个完美的代码版本。您可以查看http://start.spring.io以获得spring-data-jpa的相应gradle版本。

 类似资料: