英文:
Can interface without implementation be autowired in service class in spring boot
问题
我在YouTube上观看了一个有关Spring教程的视频[链接](https://www.youtube.com/watch?v=_Jnu_jHfQbM&list=PLmbC-xnvykcghOSOJ1ZF6ja3aOgZAgaMO&index=31),其中他没有实现 ` public interface TopicRepository extends CrudRepository<Topic, String>`,
但他仍然通过在服务类中编写以下代码来运行应用程序:
```java
@Autowired
private TopicRepository topicRepository;
然而,当我尝试相同的操作时,出现以下错误:
> io.spring.springbootstarter.topic.TopicService中的字段topicRepository需要一个类型为'io.spring.springbootstarter.topic.TopicRepository'的bean,但找不到该bean。
>
> 注入点具有以下注释:
> - @org.springframework.beans.factory.annotation.Autowired(required=true)
>
>
> 操作:
>
> 考虑在您的配置中定义一个类型为'io.spring.springbootstarter.topic.TopicRepository'的bean。
我是Spring的新手,正在使用Spring 2.3.1版本。
在下面的类中,我创建了对象:
package io.spring.springbootstarter.topic;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TopicService {
@Autowired
private TopicRepository topicRepository;
public List<Topic> getAllTopics(){
List<Topic> topics=new ArrayList<Topic>();
topicRepository.findAll().forEach(topics::add);
return topics;
}
public Optional<Topic> getTopic(String id) {
return topicRepository.findById(id);
}
public void addTopic(Topic topic) {
topicRepository.save(topic);
}
public void updateTopic(String id , Topic topic) {
topicRepository.save(topic);
}
public void deleteTopic(String id) {
topicRepository.deleteById(id);
}
}
package io.spring.springbootstarter.topic;
import org.springframework.data.repository.CrudRepository;
public interface TopicRepository extends CrudRepository<Topic, String>{
//getTopics()
//getTopic(String id)
//deleteTopic(String id)
//updateTopic(Topic t)
}
<details>
<summary>英文:</summary>
I watched a spring tutorial video on youtube [here](https://www.youtube.com/watch?v=_Jnu_jHfQbM&list=PLmbC-xnvykcghOSOJ1ZF6ja3aOgZAgaMO&index=31) in which he did not implemented ` public interface TopicRepository extends CrudRepository<Topic, String>`
still he was able to run the application by writing
@Autowired
private TopicRepository topicRepository;
in service class, but when I try the same I get an error like:
> Field topicRepository in
> io.spring.springbootstarter.topic.TopicService required a bean of type
> 'io.spring.springbootstarter.topic.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
> 'io.spring.springbootstarter.topic.TopicRepository' in your
> configuration.
I am new to spring and i am using spring 2.3.1
package io.spring.springbootstarter.topic;
import org.springframework.data.repository.CrudRepository;
public interface TopicRepository extends CrudRepository<Topic, String>{
//getTopics()
//getTopic(String id)
//deleteTopic(String id)
//updateTopic(Topic t)
}
in below class i am creating object
package io.spring.springbootstarter.topic;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class TopicService {
@Autowired
private TopicRepository topicRepository;
public List<Topic> getAllTopics(){
List<Topic> topics=new ArrayList<Topic>();
topicRepository.findAll().forEach(topics::add);
return topics;
}
public Optional<Topic> getTopic(String id) {
return topicRepository.findById(id);
}
public void addTopic(Topic topic) {
topicRepository.save(topic);
}
public void updateTopic(String id , Topic topic) {
topicRepository.save(topic);
}
public void deleteTopic(String id) {
topicRepository.deleteById(id);
}
}
</details>
# 答案1
**得分**: 3
继承 `CrudRepository` 接口的接口会被 Spring 使用[Spring 表达式语言][SpEL]实现(自动生成)。自动生成的类是 Spring beans,因此可以进行自动装配。
你可以在[官方 Spring 文档][SpringData]中找到有关 Spring 数据仓库的更多信息。
[SpEL]: https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html
[SpringData]: https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html
<details>
<summary>英文:</summary>
Interfaces extending `CrudRepository` are implemented (auto-generated) by Spring by using the [Spring Expression Language][SpEL]. The auto-generated classes are spring-beans and thus can be autowired.
You can find more information about Spring Data Repositories in the [Official Spring Documentation][SpringData].
[SpEL]: https://docs.spring.io/spring/docs/4.3.10.RELEASE/spring-framework-reference/html/expressions.html
[SpringData]: https://docs.spring.io/spring-data/data-commons/docs/1.6.1.RELEASE/reference/html/repositories.html
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论