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

SpringBoot在自定義類中調(diào)用service層mapper層方式

 更新時(shí)間:2025年03月27日 09:50:01   作者:liudachu  
這篇文章主要介紹了SpringBoot在自定義類中調(diào)用service層mapper層方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot在自定義類中調(diào)用service層mapper層

最近在整合webscoket,因?yàn)樵趙ebsocket中需要自定義websocket類,而在后端發(fā)送的信息的時(shí)候,需要調(diào)用service層mapper層的代碼,或者自己編寫一個(gè)工具類,這里在自定義類中使用 @Autowired會(huì)報(bào)空指針異常,所以不能使用普通的注入方式,百度上能用的教程很多,我這里寫一個(gè)我嘗試過能用的。

解決方案

1.上代碼

@Component
public class ServerHandler extends IoHandlerAdapter {
    @Autowired
    protected HealthDataService healthDataService;
    private static ServerHandler  serverHandler ;
    @PostConstruct //通過@PostConstruct實(shí)現(xiàn)初始化bean之前進(jìn)行的操作
    public void init() {  
        serverHandler = this;  
        serverHandler.healthDataService = this.healthDataService;        
        // 初使化時(shí)將已靜態(tài)化的testService實(shí)例化
    }  
    //測試調(diào)用
    public void test(){
        serverHandler.healthDataService.<你的service層方法>;
    }

2.說明

  • 將需要調(diào)用Spring的Service層的類通過@Component注解為組件加載;
  • 同樣通過@Autowired獲取Service層的Bean對(duì)象;
  • 為類聲明一個(gè)靜態(tài)變量,方便下一步存儲(chǔ)bean對(duì)象;
  • 劃重點(diǎn):通過注解@PostConstruct ,在初始化的時(shí)候初始化靜態(tài)對(duì)象和它的靜態(tài)成員變量healthDataService,原理是拿到service層bean對(duì)象,靜態(tài)存儲(chǔ)下來,防止被釋放。

導(dǎo)入mapper層同上

/**
	我自己編寫的工具類
*/
@Component
public class GetChartDataUtils {

    private static GetChartDataUtils getChartDataUtils ;

    @Autowired
    private OrderMapper orderMapper;

    @PostConstruct //通過@PostConstruct實(shí)現(xiàn)初始化bean之前進(jìn)行的操作
    public void init() {
        getChartDataUtils = this;
        getChartDataUtils.orderMapper=this.orderMapper;
        // 初使化時(shí)將已靜態(tài)化的orderMapper實(shí)例化
    }

    public static Trend<PosMonth> getOrgMonthTrend() {
        Trend<PosMonth> posMonthTrend = new Trend<>();
        posMonthTrend.setTitle("客源分析趨勢");
        posMonthTrend.setBase(300);
        posMonthTrend.setUnit("萬");
        List<PosMonth> posMonths = new ArrayList<PosMonth>();
        //封裝每個(gè)出發(fā)地點(diǎn)每個(gè)月的數(shù)據(jù)
        PosMonth zzgs = new PosMonth("鄭州工商學(xué)院",getChartDataUtils.orderMapper.queryOriginMonthCount("鄭州工商學(xué)院"));//出發(fā)地為鄭州工商學(xué)院每個(gè)月的數(shù)據(jù)
        PosMonth zzjt = new PosMonth("河南交通學(xué)院",getChartDataUtils.orderMapper.queryOriginMonthCount("河南交通學(xué)院"));//出發(fā)地為河南交通學(xué)院每個(gè)月的數(shù)據(jù)
        PosMonth zkzx = new PosMonth("周口中心站",getChartDataUtils.orderMapper.queryOriginMonthCount("周口中心站"));//出發(fā)地為周口中心站每個(gè)月的數(shù)據(jù)
        PosMonth zzly = new PosMonth("鄭州旅游學(xué)院",getChartDataUtils.orderMapper.queryOriginMonthCount("鄭州旅游學(xué)院"));//出發(fā)地為鄭州旅游學(xué)院每個(gè)月的數(shù)據(jù)
        //數(shù)據(jù)封裝進(jìn)趨勢圖
        posMonths.add(zzgs);
        posMonths.add(zzjt);
        posMonths.add(zkzx);
        posMonths.add(zzly);
        posMonthTrend.setData(posMonths);
        return posMonthTrend;
    }

    public static Trend<PosMonth> getDisMonthTrend() {
        Trend<PosMonth> posMonthTrend = new Trend<>();
        posMonthTrend.setTitle("去向分析趨勢");
        posMonthTrend.setBase(300);
        posMonthTrend.setUnit("萬");
        List<PosMonth> posMonths = new ArrayList();
        //封裝每個(gè)去向地點(diǎn)每個(gè)月的數(shù)據(jù)
        PosMonth zzgs = new PosMonth("周口火車站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口火車站"));//目的地為周口火車站每個(gè)月的數(shù)據(jù)
        PosMonth zzjt = new PosMonth("周口市中心站",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市中心站"));//目的地為周口市中心站每個(gè)月的數(shù)據(jù)
        PosMonth zkzx = new PosMonth("周口市淮陽",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市淮陽"));//目的地為周口市淮陽每個(gè)月的數(shù)據(jù)
        PosMonth zzly = new PosMonth("周口市項(xiàng)城",getChartDataUtils.orderMapper.queryDestinationMonthCount("周口市項(xiàng)城"));//目的地為周口市項(xiàng)城每個(gè)月的數(shù)據(jù)
        //數(shù)據(jù)封裝進(jìn)趨勢圖
        posMonths.add(zzgs);
        posMonths.add(zzjt);
        posMonths.add(zkzx);
        posMonths.add(zzly);
        posMonthTrend.setData(posMonths);
        return posMonthTrend;
    }
}

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Java 8中字符串拼接新姿勢StringJoiner詳解

    Java 8中字符串拼接新姿勢StringJoiner詳解

    在本篇文章里小編給大家整理了關(guān)于Java 8中字符串拼接新姿勢StringJoiner的詳解內(nèi)容,需要的朋友們參考下。
    2019-09-09
  • java調(diào)用中國天氣網(wǎng)api獲得天氣預(yù)報(bào)信息的方法

    java調(diào)用中國天氣網(wǎng)api獲得天氣預(yù)報(bào)信息的方法

    這篇文章主要介紹了java調(diào)用中國天氣網(wǎng)api獲得天氣預(yù)報(bào)信息的方法,可實(shí)現(xiàn)調(diào)用溫度、風(fēng)力風(fēng)向及近期天氣狀況等功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下
    2015-07-07
  • Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法

    Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法

    本篇文章主要介紹了Spring Boot使用FastJson解析JSON數(shù)據(jù)的方法,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2017-02-02
  • MybatisPlus使用@TableId主鍵id自增長無效的解決

    MybatisPlus使用@TableId主鍵id自增長無效的解決

    本文主要介紹了MybatisPlus使用@TableId主鍵id自增長無效的解決,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-04-04
  • JpaRepository如何實(shí)現(xiàn)增刪改查并進(jìn)行單元測試

    JpaRepository如何實(shí)現(xiàn)增刪改查并進(jìn)行單元測試

    這篇文章主要介紹了JpaRepository如何實(shí)現(xiàn)增刪改查并進(jìn)行單元測試,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-11-11
  • java集合類遍歷的同時(shí)如何進(jìn)行刪除操作

    java集合類遍歷的同時(shí)如何進(jìn)行刪除操作

    這篇文章主要介紹了java集合類遍歷的同時(shí)如何進(jìn)行刪除操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • springboot+nacos+gateway實(shí)現(xiàn)灰度發(fā)布的實(shí)例詳解

    springboot+nacos+gateway實(shí)現(xiàn)灰度發(fā)布的實(shí)例詳解

    灰度發(fā)布是一種在軟件部署過程中用于平滑過渡的技術(shù),通過引入灰度發(fā)布SDK和配置網(wǎng)關(guān)策略實(shí)現(xiàn),本文就來介紹一下,感興趣的可以了解一下
    2022-03-03
  • RabbitMq中channel接口的幾種常用參數(shù)詳解

    RabbitMq中channel接口的幾種常用參數(shù)詳解

    這篇文章主要介紹了RabbitMq中channel接口的幾種常用參數(shù)詳解,RabbitMQ 不會(huì)為未確認(rèn)的消息設(shè)置過期時(shí)間,它判斷此消息是否需要重新投遞給消費(fèi)者的唯一依據(jù)是消費(fèi)該消息的消費(fèi)者連接是否己經(jīng)斷開,需要的朋友可以參考下
    2023-08-08
  • kill命令在Java應(yīng)用中使用的注意事項(xiàng)小結(jié)

    kill命令在Java應(yīng)用中使用的注意事項(xiàng)小結(jié)

    這篇文章主要給大家介紹了關(guān)于kill命令在Java應(yīng)用中使用的注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • java解析excel文件的方法

    java解析excel文件的方法

    這篇文章主要介紹了java解析excel文件的方法,這里整理相關(guān)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。
    2017-02-02

最新評(píng)論