简体   繁体   English

Controller.Controller 中的错误字段存储库需要找不到类型为“Controller.Repository”的 bean

[英]Error Field Repository in Controller.Controller required a bean of type 'Controller.Repository' that could not be found

I'm trying to connect my Controller to Repository in one of the spring application but I'm getting an error saying "Field tweetRepository in TweetsController.TweetsController required a bean of type 'TweetsController.TweetRepository' that could not be found."我正在尝试将我的控制器连接到其中一个 spring 应用程序中的存储库,但我收到一条错误消息:“TweetsController 中的字段 tweetRepository。TweetsController 需要一个类型为‘TweetsController.TweetRepository’的 bean,但找不到。”

Can someone help me with this?有人可以帮我弄这个吗? Thanks in advance.提前致谢。 I've attached code samples as well.我也附上了代码示例。

TwitterApplication.java

package SpringMVC.Twitter;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;

@SpringBootApplication
@ComponentScan("AuthController")
@ComponentScan("TweetsController")
public class TwitterApplication {

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

TwitterController.java

package TweetsController;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Optional;

@RestController
public class TweetsController {

    @Autowired
    private TweetRepository tweetRepository;

    @RequestMapping("/tweets")
    public Iterable<TweetsContent> getAllTweets() {
        return tweetRepository.findAll();
    }

    @RequestMapping("tweet/{id}")
    public Optional<TweetsContent> getTweet(@PathVariable int id) {
        return tweetRepository.findById(id);
    }

    @RequestMapping(method = RequestMethod.POST, value = "/tweets")
    public boolean addTweet(@RequestBody TweetsContent tweet) {
        TweetsContent t = tweetRepository.save(new TweetsContent(tweet.getTitle(), tweet.getContent()));
        if (t != null)
            return true;
        else
            return false;
    }
}

TwitterRepository.java

package TweetsController;

import org.springframework.data.repository.CrudRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface TweetRepository extends CrudRepository<TweetsContent, Integer> { }

Add the spring data dependency on your pom:在您的 pom 上添加 spring 数据依赖项:

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

Add this too.也加上这个。 @ComponentScan("TweetRepository") @ComponentScan("TweetRepository")

You don't need to use @ComponentScan("AuthController") and @ComponentScan("TweetsController") because if you use @SpringBootApplication, scanning will occur from the package of the class that declares this annotation.您不需要使用@ComponentScan("AuthController") 和@ComponentScan("TweetsController"),因为如果您使用@SpringBootApplication,将从声明此注释的类的包中进行扫描。 But you need to put TwitterApplication like this:但是你需要像这样放置 TwitterApplication:
在此处输入图像描述

You don't have to specify @ComponentScan in your TwitterApplication , because spring boot application is scanning its own directory at the startup.您不必在TwitterApplication中指定@ComponentScan ,因为 spring boot 应用程序在启动时会扫描自己的目录。 and remove the @Repository annotation from your repository class, it is also not needed.并从您的存储库类中删除@Repository注释,它也不是必需的。

暂无
暂无

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

相关问题 com.example.controller.UserController 中的字段 userRepository 需要一个无法找到的“com.example.repository.UserRepository”类型的 bean - Field userRepository in com.example.controller.UserController required a bean of type 'com.example.repository.UserRepository' that could not be found com.aaa.controller.AbcController 中的字段 abc 需要找不到类型为“com.aaa.repository.AbcRepository”的 bean - Field abc in com.aaa.controller.AbcController required a bean of type 'com.aaa.repository.AbcRepository' that could not be found 在...中的字段存储库需要一个类型为...的bean,无法找到的NoteRepository - Field repository in …NoteController required a bean of type …NoteRepository that could not be found 创建 Bean 控制器和存储库时出错 - Error Creating Bean Controller and Repository 需要一个无法找到的 Repository 类型的 bean - Required a bean of type Repository that could not be found Controller 中构造函数的参数 0 需要一个找不到的 Service class 类型的 bean - Parameter 0 of constructor in Controller required a bean of type Service class that could not be found 存储库字段需要一个无法找到的类型的 bean - repository field required a bean of type that cannot be found Controller中的字段需要找不到名为&#39;entityManagerFactory&#39;的bean - Field in Controller required a bean named 'entityManagerFactory' that could not be found Spring 引导 - 存储库字段需要一个名为“entityManagerFactory”的 bean,但找不到 - Spring Boot - repository field required a bean named 'entityManagerFactory' that could not be found 服务中构造函数的参数 0 需要无法找到类型存储库的 bean - Parameter 0 of constructor in service required a bean of type repository that could not be found
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM