spring boot 启动失败--在配置中定义一个'TopicRepository'类型的bean
spring boot fails to start-- define a bean of type 'TopicRepository' in configuration
我正在关注 this JavaBrains Spring Boot 教程。
我的项目结构如下:
CourseApiApp.java:
@SpringBootApplication
@ComponentScan(basePackages = {
"com.bloodynacho.rishab.topics"
})
@EntityScan("com.bloodynacho.rishab.topics")
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
TopicController.java:
@RestController
public class TopicController {
@Autowired
private TopicService topicService;
@RequestMapping(
value = "/topics"
)
public List<Topic> getAllTopcs() {
return topicService.getAllTopics();
}
}
TopicService.java:
@Service
public class TopicService {
@Autowired
private TopicRepository topicRepository;
public List<Topic> getAllTopics() {
List<Topic> topics = new ArrayList<>();
this.topicRepository
.findAll()
.forEach(topics::add);
return topics;
}
}
Topic.java:
@Entity
public class Topic {
@Id
private String id;
private String name;
private String description;
}
TopicRepository.java:
@Repository
public interface TopicRepository extends CrudRepository<Topic, String>{
}
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我在 Topic.java
中使用了 lombok @Getter
、@Getter
和 @AllArgsConstructor
,但我在阅读 的一个答案后将其删除。
我读了, ,
不过,我明白了
***************************
APPLICATION FAILED TO START
***************************
Description:
Field topicRepository in com.bloodynacho.rishab.topics.TopicService required a bean of type 'com.bloodynacho.rishab.topics.TopicRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.bloodynacho.rishab.topics.TopicRepository' in your configuration.
Process finished with exit code 1
编辑:我读过 解释了即使没有实际实现接口,@Autowired 也是如何工作的。我了解解决方案,但我不了解如何解决我的问题。显然,Spring 数据设置和配置的方式存在一些问题(如答案中所述)
因为如果您的其他包层次结构位于带有 @SpringBootApplication
注释的主应用程序之下,您将被隐式组件扫描覆盖。
因此,一个简单的解决方案可以通过以下 2 个步骤完成:
- 将 main class 的包重命名为
com.bloodynacho.rishab
。
(这就是我建议主应用程序的完整包名称应该是其他包的根。)
- 删除
@ComponentScan
和 @EntityScan
注释。
(虽然@ComponentScan
和@EntityScan
不一样,但是根据我的经验也是可以去掉的。)
我正在关注 this JavaBrains Spring Boot 教程。
我的项目结构如下:
CourseApiApp.java:
@SpringBootApplication
@ComponentScan(basePackages = {
"com.bloodynacho.rishab.topics"
})
@EntityScan("com.bloodynacho.rishab.topics")
public class CourseApiApp {
public static void main(String[] args) {
SpringApplication.run(CourseApiApp.class, args);
}
}
TopicController.java:
@RestController
public class TopicController {
@Autowired
private TopicService topicService;
@RequestMapping(
value = "/topics"
)
public List<Topic> getAllTopcs() {
return topicService.getAllTopics();
}
}
TopicService.java:
@Service
public class TopicService {
@Autowired
private TopicRepository topicRepository;
public List<Topic> getAllTopics() {
List<Topic> topics = new ArrayList<>();
this.topicRepository
.findAll()
.forEach(topics::add);
return topics;
}
}
Topic.java:
@Entity
public class Topic {
@Id
private String id;
private String name;
private String description;
}
TopicRepository.java:
@Repository
public interface TopicRepository extends CrudRepository<Topic, String>{
}
pom.xml:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.apache.derby</groupId>
<artifactId>derby</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
我在 Topic.java
中使用了 lombok @Getter
、@Getter
和 @AllArgsConstructor
,但我在阅读
我读了
不过,我明白了
***************************
APPLICATION FAILED TO START
***************************
Description:
Field topicRepository in com.bloodynacho.rishab.topics.TopicService required a bean of type 'com.bloodynacho.rishab.topics.TopicRepository' that could not be found.
The injection point has the following annotations:
- @org.springframework.beans.factory.annotation.Autowired(required=true)
Action:
Consider defining a bean of type 'com.bloodynacho.rishab.topics.TopicRepository' in your configuration.
Process finished with exit code 1
编辑:我读过
因为如果您的其他包层次结构位于带有 @SpringBootApplication
注释的主应用程序之下,您将被隐式组件扫描覆盖。
因此,一个简单的解决方案可以通过以下 2 个步骤完成:
- 将 main class 的包重命名为
com.bloodynacho.rishab
。
(这就是我建议主应用程序的完整包名称应该是其他包的根。) - 删除
@ComponentScan
和@EntityScan
注释。
(虽然@ComponentScan
和@EntityScan
不一样,但是根据我的经验也是可以去掉的。)