SpringBoot調(diào)用service層的三種方法
一、在Controller中調(diào)用
1、定義Service接口:
首先,你需要定義一個(gè)Service接口,其中包含你需要實(shí)現(xiàn)的方法。
public interface MyService {
String doSomething();
}2、實(shí)現(xiàn)Service接口:
@Service
public class MyServiceImpl implements MyService {
@Override
public String doSomething() {
return "Something done!";
}
}注意@Service注解,它告訴Spring這是一個(gè)Service組件,應(yīng)該被管理在Spring容器中。
3、在Controller中注入Service:
在你的Controller中,你可以使用@Autowired或@Inject注解來(lái)注入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)你訪問(wèn)/api/dosomething時(shí),Controller會(huì)調(diào)用Service層中的doSomething()方法。
二、從其他組件調(diào)用Service:
如果你需要從其他非Controller組件(如另一個(gè)Service或組件)調(diào)用Service層的方法,你可以通過(guò)相同的方式注入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
}
}三、從啟動(dòng)類(lèi)中調(diào)用Service:
1、編寫(xiě)一個(gè)SpringUtil工具類(lèi):
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;
}
//通過(guò)name獲取 Bean.
public static Object getBean(String name){
return getApplicationContext().getBean(name);
}
//通過(guò)class獲取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
}
//通過(guò)name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
}
}
這樣就可以從容器中獲取組件了
在啟動(dòng)類(lèi)寫(xiě) 下代碼:
//注入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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Maven構(gòu)建時(shí)跳過(guò)部分測(cè)試的實(shí)例
下面小編就為大家分享一篇Maven構(gòu)建時(shí)跳過(guò)部分測(cè)試的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助2017-11-11
idea 默認(rèn)路徑修改從C盤(pán)更改到D盤(pán)
本文主要介紹了idea 默認(rèn)路徑修改從C盤(pán)更改到D盤(pán),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07
如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問(wèn)404
在本篇文章里小編給大家整理的是一篇關(guān)于如何解決SpringBoot集成百度UEditor圖片上傳后直接訪問(wèn)404相關(guān)文章,需要的朋友們學(xué)習(xí)下。2019-11-11
java多線程之并發(fā)工具類(lèi)CountDownLatch,CyclicBarrier和Semaphore
這篇文章主要為大家介紹了java并發(fā)工具類(lèi)CountDownLatch,CyclicBarrier和Semaphore ,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助2021-12-12
mybatis通過(guò)中間表實(shí)現(xiàn)一對(duì)多查詢功能
這篇文章主要介紹了mybatis通過(guò)中間表實(shí)現(xiàn)一對(duì)多查詢,通過(guò)一個(gè)學(xué)生的id查詢出該學(xué)生所學(xué)的所有科目,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2021-08-08
Spring Boot統(tǒng)一異常攔截實(shí)踐指南(最新推薦)
本文介紹了Spring Boot中統(tǒng)一異常處理的重要性及實(shí)現(xiàn)方案,包括使用`@ControllerAdvice`和`@ExceptionHandler`注解,實(shí)現(xiàn)全局異常處理和統(tǒng)一響應(yīng)格式,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2025-02-02

