SpringBoot如何實現(xiàn)調用controller和Service層方法
更新時間:2025年03月08日 14:04:48 作者:北極的企鵝88
文章介紹了在SpringBoot中如何在工具類中調用Controller和Service層的方法,通過創(chuàng)建一個工具類SpringUtil,并在Spring?Boot啟動類中進行配置掃描注入,工具類就可以訪問Controller和Service層的方法
SpringBoot調用controller和Service層方法
說明
- 最近遇到一個問題,如何在工具類中去訪問controller層與service層的方法。
- 因為平時在調用service層時都是在controller中,有配置掃描注入,spring會根據(jù)配置自動注入所依賴的服務層。
- 但因我們寫的工具類不屬于controller層,所以當所寫接口需要調用服務層是,常常會為NULL。
實現(xiàn)
1.創(chuàng)建一個工具類:SpringUtil
package com.ruoyi.web.controller.employment.utils; 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); } }
2.SpringBoot啟動類調用controller和service方法
package com.ruoyi; import com.ruoyi.web.controller.employment.ProvportController1; import com.ruoyi.web.controller.employment.ProvportController2; import com.ruoyi.web.controller.employment.utils.SpringUtil; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.context.ApplicationContext; /** * 啟動程序 * * @author ruoyi */ @SpringBootApplication(exclude = { DataSourceAutoConfiguration.class }) public class RuoYiApplication { //注入service private static TestService testService; //注入controller private static TestController testController; public static void main(String[] args) throws InterruptedException { SpringApplication.run(RuoYiApplication.class, args); ApplicationContext context = SpringUtil.getApplicationContext(); testService= context.getBean(testService.class); testController= context.getBean(testController.class); //調用service方法 testService.getService(); //調用controller方法 testController.getController(); } }
總結
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- springboot中的controller參數(shù)映射問題小結
- springboot中Controller內文件上傳到本地及阿里云操作方法
- springboot如何通過controller層實現(xiàn)頁面切換
- springboot Controller直接返回String類型帶來的亂碼問題及解決
- SpringBoot之controller參數(shù)校驗詳解
- springboot中@RestController注解實現(xiàn)
- SpringBoot通過注解監(jiān)測Controller接口的代碼示例
- springboot controller參數(shù)注入方式
- SpringBoot中@RestControllerAdvice @ExceptionHandler異常統(tǒng)一處理類失效原因分析
- SpringBoot和MybatisPlus實現(xiàn)通用Controller示例
相關文章
Java concurrency之非公平鎖_動力節(jié)點Java學院整理
本篇文章主要介紹了Java concurrency之非公平鎖,詳細的介紹了獲取和釋放非公平鎖,有興趣的同學可以了解一下2017-06-06java中double轉化為BigDecimal精度缺失的實例
下面小編就為大家?guī)硪黄猨ava中double轉化為BigDecimal精度缺失的實例。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03Springboot 整合 Java DL4J 實現(xiàn)智能客服功能
本文主要介紹了如何使用SpringBoot整合JavaDeeplearning4j來構建一個智能客服系統(tǒng),詳細探討了神經(jīng)網(wǎng)絡選擇、數(shù)據(jù)集格式、技術介紹、Maven依賴、代碼示例等內容,為構建高效、便捷、個性化的客戶服務提供了理論支持和實踐指導2024-10-10