能否在Spring Boot的服务类中自动连线没有实现的接口

huangapple go评论84阅读模式
英文:

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&amp;list=PLmbC-xnvykcghOSOJ1ZF6ja3aOgZAgaMO&amp;index=31) in which he did not implemented ` public interface TopicRepository extends CrudRepository&lt;Topic, String&gt;`
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:

&gt; Field topicRepository in
&gt; io.spring.springbootstarter.topic.TopicService required a bean of type
&gt; &#39;io.spring.springbootstarter.topic.TopicRepository&#39; that could not be
&gt; found.
&gt; 
&gt; The injection point has the following annotations:
&gt; 	- @org.springframework.beans.factory.annotation.Autowired(required=true)
&gt; 
&gt; 
&gt; Action:
&gt; 
&gt; Consider defining a bean of type
&gt; &#39;io.spring.springbootstarter.topic.TopicRepository&#39; in your
&gt; 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&lt;Topic, String&gt;{

	
	   //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&lt;Topic&gt; getAllTopics(){
		
		List&lt;Topic&gt; topics=new ArrayList&lt;Topic&gt;();
		topicRepository.findAll().forEach(topics::add);
		 return topics;
	}
	
	public Optional&lt;Topic&gt; 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>



huangapple
  • 本文由 发表于 2020年7月26日 04:46:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/63093417.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定