【发布时间】:2019-10-30 14:48:35
【问题描述】:
我在关注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,但在阅读here 的其中一个答案后我将其删除。
我还是明白了
***************************
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
编辑:我读到this 解释了即使没有实际实现接口,@Autowired 也是如何工作的。我理解解决方案,但我不明白如何解决我的问题。显然,Spring Data 的设置和配置方式存在一些问题(如答案中所述)
【问题讨论】:
-
TopicRepository 不应该是一个接口,而应该是一个类。
-
你确定吗?如果我做 public class TopicRepository extends CrudRepository
{} 我在 IntelliJ 中得到一个编译错误,说 Class TopicRepository 必须被声明为抽象或在 CrudRepository 中实现抽象方法 save(S)。另外,我查看了教程视频,还有它的界面,而不是一个类。 -
尝试创建一个虚拟方法 save(S) 并使其成为一个类。它应该可以工作。
-
@LHCHIN 会删除编译错误但那是错误的,因为
@SpringBootApplication仅在使用它的包中查找包,因此删除@ComponentScan基本上意味着,Spring 不知道主题和课程包,看这个答案stackoverflow.com/questions/40384056/… -
当然,我的意思是如果你将主类的包名设置为
com.bloodynacho.rishab,那么你不需要添加这两个注释。而且在我看来,主类的包名应该是其他包的根名。
标签: spring spring-boot spring-data-jpa derby lombok