Spring Boot中服务接口类的用途 [英] Purpose of Service Interface Class in Spring Boot

查看:48
本文介绍了Spring Boot中服务接口类的用途的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是关于接口类的使用.我对Spring还是很陌生,所以如果这太简单了,请忍受.

My question is regarding the use of the interface class. I am fairly new to Spring so please bear with me if this is overly simple.

首先,当您可以在BoxService中声明全部查找内容时,在此处具有IBoxService接口有什么意义?其次,在控制器中如何使用IBoxService.意思是,我们正在调用IBoxService.findAll().但是,这如何与BoxService类联系在一起.如果多个服务类实现了IBoxService,该怎么办?这是java还是Spring注入.谢谢.

First of all, what is the point of having an IBoxService interface here when you could just declare the find all in BoxService. Secondly, in the controller how is IBoxService being used. Meaning, we are calling IBoxService.findAll(). But, how is this being tied to the BoxService class. What if multiple service classes implemented IBoxService? Is this a java thing or a Spring injection thing. Thanks.

package com.xyz.service;

import com.xyz.model.Box;
import java.util.Set;

public interface IBoxService {

    Set<Box> findAll();
}

package com.xyz.service;

import com.xyz.model.Box;
import com.xyz.repository.BoxRepository;
import java.util.Set;
import org.springframework.stereotype.Service;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;

@Service
@AllArgsConstructor
@Slf4j
@Transactional
public class BoxService implements IBoxService {
@Autowired
private BoxRepository boxRepo;

@Override
public Set<City> findAll() {

    return (Set<City>) repository.findAll();
}

}

package com.xyz.controller;

import com.xyz.model.Box;
import com.xyz.service.IBoxService;
import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;    

@RestController
@RequestMapping("/api/box")
public class BoxController {

    @Autowired
    private IBoxService boxService;

    @GetMapping
    public ResponseEntity<Set<Boxes>> allBoxes() {

        return (Set<Box>) boxService.findAll();
    }
}

推荐答案

请理解基本的Java和接口的用法.Spring Boot只是Java上的抽象,因此所有基本概念都照原样适用.

Please understand basic Java and use of interface . Spring boot is just abstraction over Java hence all the basic concepts applies as it is.

回到您的问题,IBoxService是一个接口,该接口允许在控制器级别注入所需的实现.到目前为止,只有IBoxServic的实现是BoxService,因此它会自动注入.如果您有多个实现,则需要使用限定符注释来指定需要注入的实现类型.或者,您也可以使用类名称自己创建对象

Coming back to your questions IBoxService is a interface which allows to inject required implementation of it at controller level. As of now only implementation of IBoxServic is BoxService hence it is getting injected automatically. In case you have multiple implementations you need to use qualifier annotation to specify kind of implementation you need to inject. Or you can create object bu yourself using class names

请考虑以下内容:

IBoxService由BoxService和TiffinBoxService两个类实现

IBoxService is implemented by two classes BoxService and TiffinBoxService

现在在控制器中,您可以注入所需的实现.这使我们可以实现隐藏内部细节的接口原理.

Now in controller you can inject implementation which you want. Which allow us to achieve principle of interface which is hide internal details.

在这种情况下,作为控制器的用户不需要知道内部正在使用哪个类,因为我们正在使用接口的引用.

User which is controller in this case doesn't need to know which class is being use internally as we are using reference of interface.

列表接口是最好的示例,它以ArrayList和LinkedList作为实现类.

List interface is best example which has ArrayList and LinkedList as implementation classes.

希望它有用!

这篇关于Spring Boot中服务接口类的用途的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆