[英]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.