SpringBoot在啟動類main方法中調(diào)用service層方法報(bào)“空指針異常“的解決辦法
大多數(shù)情況下,我們使用Springboot是創(chuàng)建一個maven項(xiàng)目,然后通過controller層的接口調(diào)用。但也有特殊情況,比如將需要傳參的接口直接打包成可執(zhí)行jar包運(yùn)行,這個時(shí)候,就需要在啟動類main方法中注入Bean,調(diào)用Service層方法使用。
按照常規(guī)service方法調(diào)用實(shí)現(xiàn),報(bào)錯如下:

報(bào)錯原因:是service注入為空,service無法導(dǎo)入到非controller層中去
解決方法:注入bean
1、首先建立一個Spring工具類:SpringUtil
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);
}
}
ApplicationContextAware :當(dāng)一個類實(shí)現(xiàn)ApplicationContextAware接口后,這個類就可以方便獲得ApplicationContext中所有的bean。簡言之,就是這個類可以直接獲取spring配置文件中所有有引用到的bean對象。
ApplicationContext:ApplicationContext是由BeanFactory派生而來的,BeanFactory負(fù)責(zé)配置、創(chuàng)建、管理Bean,是Spring容器最基本的接口;
BeanFactory的許多功能需要變成實(shí)現(xiàn),而ApplicationContext中則可以通過配置的方式實(shí)現(xiàn),即:
在構(gòu)建容器的時(shí)候,ApplicationContext創(chuàng)建對象采用的策略是立即加載的方式,即只要一讀取完配置文件就立即創(chuàng)建配置文件中配置的對象。BeanFactory采用的是延遲加載的方式,什么時(shí)候根據(jù)id獲取對象了,什么時(shí)候才真正地創(chuàng)建對象。
2、在main方法中調(diào)用service,注意要啟動入口類 SpringApplication.run(TextCutApplication.class, args);
加載Spring配置文件時(shí),如果Spring配置文件中所定義的類實(shí)現(xiàn)了ApplicationContextAware接口,那么在加載Spring配置文件時(shí),會自動調(diào)用ApplicationContextAware接口中的setApplicationContext方法,獲得ApplicationContext對象。
ApplicationContext對象是由Spring注入的,前提必須在Spring配置文件中指定該類。

到此這篇關(guān)于SpringBoot在啟動類main方法中調(diào)用service層方法報(bào)“空指針異常“的解決辦法的文章就介紹到這了,更多相關(guān)SpringBoot service空指針異常內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Springboot微服務(wù)分布式框架Rouyi Cloud權(quán)限認(rèn)證(登錄流程之token解析)
這篇文章主要介紹了Springboot微服務(wù)分布式框架Rouyi Cloud權(quán)限認(rèn)證的相關(guān)知識,重點(diǎn)講解下整個框架的入口,登錄流程之token解析,感興趣的朋友跟隨小編一起看看吧2024-04-04
使用SpringBoot+EasyExcel+Vue實(shí)現(xiàn)excel表格的導(dǎo)入和導(dǎo)出詳解
這篇文章主要介紹了使用SpringBoot+VUE+EasyExcel?整合導(dǎo)入導(dǎo)出數(shù)據(jù)的過程詳解,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08
Spring Native實(shí)現(xiàn)0.059s啟動一個SpringBoot項(xiàng)目
Spring Native是Spring框架的一個子項(xiàng)目,旨在提供一種將Spring應(yīng)用程序編譯為本地可執(zhí)行文件的方法,從而提高啟動時(shí)間和資源效率,本文主要介紹了Spring Native實(shí)現(xiàn)0.059s啟動一個SpringBoot項(xiàng)目,感興趣的可以了解一下2024-02-02

