欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot調(diào)用service層的三種方法

 更新時間:2024年05月15日 08:57:13   作者:獨自成長  
在Spring?Boot中,我們可以通過注入Service層對象來調(diào)用Service層的方法,Service層是業(yè)務(wù)邏輯的處理層,它通常包含了對數(shù)據(jù)的增刪改查操作,本文給大家介紹了SpringBoot調(diào)用service層的三種方法,需要的朋友可以參考下

一、在Controller中調(diào)用

1、定義Service接口:

首先,你需要定義一個Service接口,其中包含你需要實現(xiàn)的方法。

public interface MyService {  
    String doSomething();  
}

2、實現(xiàn)Service接口:

@Service  
public class MyServiceImpl implements MyService {  
    @Override  
    public String doSomething() {  
        return "Something done!";  
    }  
}

注意@Service注解,它告訴Spring這是一個Service組件,應(yīng)該被管理在Spring容器中。

3、在Controller中注入Service:

在你的Controller中,你可以使用@Autowired@Inject注解來注入Service。

@RestController  
@RequestMapping("/api")  
public class MyController {  
 
    private final MyService myService;  
 
    @Autowired  
    public MyController(MyService myService) {  
        this.myService = myService;  
    }  
 
    @GetMapping("/dosomething")  
    public ResponseEntity<String> doSomething() {  
        String result = myService.doSomething();  
        return ResponseEntity.ok(result);  
    }  
}

現(xiàn)在,當(dāng)你訪問/api/dosomething時,Controller會調(diào)用Service層中的doSomething()方法。

二、從其他組件調(diào)用Service:

如果你需要從其他非Controller組件(如另一個Service或組件)調(diào)用Service層的方法,你可以通過相同的方式注入Service。

@Component  
public class AnotherComponent {  
 
    private final MyService myService;  
 
    @Autowired  
    public AnotherComponent(MyService myService) {  
        this.myService = myService;  
    }  
 
    public void someMethod() {  
        String result = myService.doSomething();  
        // Do something with the result  
    }  
}

三、從啟動類中調(diào)用Service:

1、編寫一個SpringUtil工具類:

package com.example.iotdemo.util;
 
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
@Component
public class SpringUtil implements ApplicationContextAware {
    private static ApplicationContext applicationContext = null;
 
 
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        if(SpringUtil.applicationContext == null){
            SpringUtil.applicationContext  = applicationContext;
        }
 
    }
 
    //獲取applicationContext
    public static ApplicationContext getApplicationContext() {
        return applicationContext;
    }
 
    //通過name獲取 Bean.
    public static Object getBean(String name){
        return getApplicationContext().getBean(name);
 
    }
 
    //通過class獲取Bean.
    public static <T> T getBean(Class<T> clazz){
        return getApplicationContext().getBean(clazz);
    }
 
    //通過name,以及Clazz返回指定的Bean
    public static <T> T getBean(String name,Class<T> clazz){
        return getApplicationContext().getBean(name, clazz);
    }
}
 

這樣就可以從容器中獲取組件了

在啟動類寫 下代碼:

    //注入service
    private static MyService myService;
 
    public static void main(String[] args) throws InterruptedException {
        SpringApplication.run(TestApplication.class, args);
        ApplicationContext context = SpringUtil.getApplicationContext();
        myService= context.getBean(MyService.class);
        //調(diào)用service方法
        myService.someMethod();            
    }

到此這篇關(guān)于SpringBoot調(diào)用service層的三種方法的文章就介紹到這了,更多相關(guān)SpringBoot調(diào)用service內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Maven構(gòu)建時跳過部分測試的實例

    Maven構(gòu)建時跳過部分測試的實例

    下面小編就為大家分享一篇Maven構(gòu)建時跳過部分測試的實例,具有很好的參考價值,希望對大家有所幫助
    2017-11-11
  • idea 默認路徑修改從C盤更改到D盤

    idea 默認路徑修改從C盤更改到D盤

    本文主要介紹了idea 默認路徑修改從C盤更改到D盤,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2024-07-07
  • spring boot打包成可執(zhí)行jar包

    spring boot打包成可執(zhí)行jar包

    本篇文章主要介紹了spring boot打包成可執(zhí)行jar包,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-10-10
  • Java內(nèi)部類的繼承(全)

    Java內(nèi)部類的繼承(全)

    這篇文章主要介紹了Java內(nèi)部類的繼承,大家都知道JAVA內(nèi)部類的構(gòu)造器必須連接指向其外圍類對象的引用,所以在繼承內(nèi)部類的時候,需要在導(dǎo)出類的構(gòu)造器中手動加入對基類構(gòu)造器的調(diào)用,需要的朋友可以參考下
    2015-07-07
  • Spring中@Scope注解用法解析

    Spring中@Scope注解用法解析

    這篇文章主要介紹了Spring中@Scope注解用法解析,@Scope注解主要作用是調(diào)節(jié)Ioc容器中的作用域,在Spring IoC容器中主要有以下五種作用域,需要的朋友可以參考下
    2023-11-11
  • 如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問404

    如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問404

    在本篇文章里小編給大家整理的是一篇關(guān)于如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問404相關(guān)文章,需要的朋友們學(xué)習(xí)下。
    2019-11-11
  • java多線程之并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore

    java多線程之并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore

    這篇文章主要為大家介紹了java并發(fā)工具類CountDownLatch,CyclicBarrier和Semaphore ,具有一定的參考價值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來幫助
    2021-12-12
  • 必須要學(xué)會的JMM與volatile

    必須要學(xué)會的JMM與volatile

    這篇文章主要介紹了必須要學(xué)會的JMM與volatile,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-09-09
  • mybatis通過中間表實現(xiàn)一對多查詢功能

    mybatis通過中間表實現(xiàn)一對多查詢功能

    這篇文章主要介紹了mybatis通過中間表實現(xiàn)一對多查詢,通過一個學(xué)生的id查詢出該學(xué)生所學(xué)的所有科目,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下
    2021-08-08
  • Spring Boot統(tǒng)一異常攔截實踐指南(最新推薦)

    Spring Boot統(tǒng)一異常攔截實踐指南(最新推薦)

    本文介紹了Spring Boot中統(tǒng)一異常處理的重要性及實現(xiàn)方案,包括使用`@ControllerAdvice`和`@ExceptionHandler`注解,實現(xiàn)全局異常處理和統(tǒng)一響應(yīng)格式,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧
    2025-02-02

最新評論