解決Springboot @Autowired 無(wú)法注入問(wèn)題
特別提醒:一定要注意文件結(jié)構(gòu)
WebappApplication 一定要在包的最外層,否則Spring無(wú)法對(duì)所有的類進(jìn)行托管,會(huì)造成@Autowired 無(wú)法注入。
1. 添加工具類獲取在 Spring 中托管的 Bean
?。?)工具類
package com.common; import org.springframework.beans.BeansException; import org.springframework.beans.factory.NoSuchBeanDefinitionException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; /** * @program: IPC_1P * @description: 獲取在spring中托管的bean * @author: johnny * @create: 2018-08-03 16:24 **/ public class SpringContextUtil { private static ApplicationContext applicationContext; // Spring應(yīng)用上下文 // 下面的這個(gè)方法上加了@Override注解,原因是繼承ApplicationContextAware接口是必須實(shí)現(xiàn)的方法 public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException { SpringContextUtil.applicationContext = applicationContext; } public static ApplicationContext getApplicationContext() { return applicationContext; } public static Object getBean(String name) throws BeansException { return applicationContext.getBean(name); } public static Object getBean(String name, Class requiredType) throws BeansException { return applicationContext.getBean(name, requiredType); } public static boolean containsBean(String name) { return applicationContext.containsBean(name); } public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return applicationContext.isSingleton(name); } public static Class getType(String name) throws NoSuchBeanDefinitionException { return applicationContext.getType(name); } public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return applicationContext.getAliases(name); } }
?。?)使用
1)程序啟動(dòng)時(shí),實(shí)例化 SpringContextUtil
@SpringBootApplication public class WebappApplication { private static ApplicationContext applicationContext; public static void main(String[] args) { applicationContext = SpringApplication.run(WebappApplication.class, args); // SpringContextUtil springContextUtil = new SpringContextUtil(); springContextUtil.setApplicationContext(applicationContext); System.out.println("服務(wù)器啟動(dòng)測(cè)試!"); }
2)在使用 @Service 的方法中,通過(guò)@Autowired 注入,使用SpringcontexUtil 獲取Bean上下文
@Autowired SenderService senderService; public class Package_State { @Autowired SenderService senderService; @Component private Package_State() { senderService = (SenderService)SpringContextUtil.getBean("senderService"); } }
總結(jié)
以上所述是小編給大家介紹的解決Springboot @Autowired 無(wú)法注入問(wèn)題,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Spring boot啟動(dòng)流程之解決循環(huán)依賴的方法
循環(huán)依賴,指的是兩個(gè)bean之間相互依賴,形成了一個(gè)循環(huán),spring解決循環(huán)依賴的方式是在bean的實(shí)例化完成之后,所以不要在構(gòu)造方法中引入循環(huán)依賴,因?yàn)檫@時(shí)對(duì)象還沒(méi)有實(shí)例化,spring也無(wú)法解決,本文給大家介紹Spring boot循環(huán)依賴的解決方法,一起看看吧2024-02-02SpringBoot快速入門及起步依賴解析(實(shí)例詳解)
SpringBoot?是由?Pivotal?團(tuán)隊(duì)提供的全新框架,其設(shè)計(jì)目的是用來(lái)簡(jiǎn)化?Spring?應(yīng)用的初始搭建以及開(kāi)發(fā)過(guò)程,這篇文章主要介紹了SpringBoot快速入門及起步依賴解析,需要的朋友可以參考下2022-10-10Jpa?Specification如何實(shí)現(xiàn)and和or同時(shí)使用查詢
這篇文章主要介紹了Jpa?Specification如何實(shí)現(xiàn)and和or同時(shí)使用查詢,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11Spring Security+JWT實(shí)現(xiàn)認(rèn)證與授權(quán)的實(shí)現(xiàn)
本文主要介紹了Spring Security+JWT實(shí)現(xiàn)認(rèn)證與授權(quán)的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04SpringBoot自定義加載yml實(shí)現(xiàn)方式,附源碼解讀
這篇文章主要介紹了SpringBoot自定義加載yml實(shí)現(xiàn)方式附源碼解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03SpringBoot 統(tǒng)一公共返回類的實(shí)現(xiàn)
本文主要介紹了SpringBoot 統(tǒng)一公共返回類的實(shí)現(xiàn),配置后臺(tái)的統(tǒng)一公共返回類,這樣做目的是為了統(tǒng)一返回信息,文中示例代碼介紹的很詳細(xì),感興趣的可以了解一下2022-01-01