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

SpringBoot如何使用ApplicationContext獲取bean對(duì)象

 更新時(shí)間:2021年11月16日 11:55:57   作者:fwhui  
這篇文章主要介紹了SpringBoot 如何使用ApplicationContext獲取bean對(duì)象,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

使用ApplicationContext獲取bean對(duì)象

編寫一個(gè)ApplicationContextFactory工廠類

public class ApplicationContextFactory{
 private static ApplicationContext applicationContext = null;
 public static void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  applicationContext = applicationContext;
 }
 public static ApplicationContext getApplicationContext(){
  return applicationContext;
 }
}

在SpringBoot的啟動(dòng)類中設(shè)置ApplicationContext

public class Application {
 public static void main(String[] args) {
  ConfigurableApplicationContext app = SpringApplication.run(Application.class, args);
  ApplicationContextFactory.setApplicationContext(app);
 }
}

通過(guò)ApplicationContextFactory獲取SpringApplication從而獲取bean對(duì)象

ApplicationContext applicationContext=ApplicationContextFactory.getApplicationContext();
Clazz clazz = applicationContext.getBean(Clazz.class);

SpringBoot Bean注入的深入研究

下面代碼可正常運(yùn)行

DemoService

@Service
public class DemoService {
    public void save(){
        System.out.println("DemoService save");
    }
}

CommonClass

@Component
public class CommonClass {
    @Resource
    private DemoService demoService;
    public void fun(){
        System.out.println("fun");
        demoService.save();
    }
}

Controller

@Resource
private CommonClass commonClass;
@ResponseBody
@GetMapping("/fun")
public void fun(){
    commonClass.fun();
}

下面代碼不能正常運(yùn)行

DemoService

@Service
public class DemoService {
    public void save(){
        System.out.println("DemoService save");
    }
}

CommonClass

public class CommonClass {
    @Resource
    private DemoService demoService;
    public void fun(){
        System.out.println("fun");
        demoService.save();
    }
}

Controller

@ResponseBody
@GetMapping("/fun")
public void fun(){
    CommonClass commonClass = new CommonClass();
    commonClass.fun();
}

比較

比較兩個(gè)代碼發(fā)現(xiàn)后者與前者的區(qū)別:因后者的CommonClass 沒(méi)有使用@Component標(biāo)注,所以在Controller中不能才用注入方式生成CommonClass對(duì)象,而是才用new的方式生成了該對(duì)象。

這樣一來(lái),CommonClass 對(duì)象是手工創(chuàng)建,所以在它內(nèi)部注入DemoService 對(duì)象的代碼就錯(cuò)誤了。

解決方案

新建工具類

@Component
public class ApplicationContextUtil implements ApplicationContextAware {
    private static  ApplicationContext act;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        act = applicationContext;
    }
    /**
     * 根據(jù)bean的名字獲取工廠中對(duì)應(yīng)的bean對(duì)象
     * @param beanName
     * @return
     */
    public static Object getBean(String beanName){
        return act.getBean(beanName);
    }
}

:實(shí)際測(cè)試發(fā)現(xiàn)上面代碼中的static不能省略

DemoService

@Service
public class DemoService {
    public void save(){
        System.out.println("DemoService save");
    }
}

CommonClass

public class CommonClass {
    @Resource
    private DemoService demoService;
    public void fun(){
    DemoService demoService = (DemoService) ApplicationContextUtil.getBean("demoService");
        System.out.println("fun");
        demoService.save();
    }
}

此處不再采用注入的方式獲取DemoService對(duì)象,而是通過(guò)工具類的方式

Controller

@ResponseBody
@GetMapping("/fun")
public void fun(){
    CommonClass commonClass = new CommonClass();
    commonClass.fun();
}

再次運(yùn)行程序,一切正常

應(yīng)用

在SpringBoot整合Shiro的案例中,自定義Realm時(shí),需要使用Service的對(duì)象。因?yàn)樽远x的Realm類不能使用@Component之類的注解注釋,所以使用本案例介紹的方法是正確的解決方案。盡管在1.6.0的shiro-all中下面代碼可以正確運(yùn)行:

在這里插入圖片描述

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

相關(guān)文章

  • java實(shí)現(xiàn)的RC4加密解密算法示例

    java實(shí)現(xiàn)的RC4加密解密算法示例

    這篇文章主要介紹了java實(shí)現(xiàn)的RC4加密解密算法,結(jié)合具體實(shí)例形式分析了java RC4加密解密算法的實(shí)現(xiàn)與使用技巧,需要的朋友可以參考下
    2017-06-06
  • 深入了解Java中Synchronized的各種使用方法

    深入了解Java中Synchronized的各種使用方法

    在Java當(dāng)中synchronized關(guān)鍵字通常是用來(lái)標(biāo)記一個(gè)方法或者代碼塊。本文將通過(guò)示例為大家詳細(xì)介紹一下Synchronized的各種使用方法,需要的可以參考一下
    2022-08-08
  • SpringBoot集成Swagger構(gòu)建api文檔的操作

    SpringBoot集成Swagger構(gòu)建api文檔的操作

    這篇文章主要介紹了SpringBoot集成Swagger構(gòu)建api文檔的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • MyBatis-Plus+Druid配置及應(yīng)用詳解

    MyBatis-Plus+Druid配置及應(yīng)用詳解

    這篇文章主要介紹了MyBatis-Plus+Druid配置及應(yīng)用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-11-11
  • Java中如何執(zhí)行多條shell/bat命令

    Java中如何執(zhí)行多條shell/bat命令

    這篇文章主要介紹了Java中如何執(zhí)行多條shell/bat命令的方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-08-08
  • Spring的跨域的幾個(gè)方案

    Spring的跨域的幾個(gè)方案

    這篇文章主要介紹了Spring的跨域的幾個(gè)方案,CrossOrigin、addCorsMappings、CorsFIlter等方案,具有一定的參考價(jià)值,需要的小伙伴可以參考一下,希望對(duì)你有所幫助
    2022-02-02
  • Spring Boot企業(yè)常用的starter示例詳解

    Spring Boot企業(yè)常用的starter示例詳解

    這篇文章主要給大家介紹了關(guān)于Spring Boot企業(yè)常用starter的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能

    springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能

    對(duì)于大文件的處理,無(wú)論是用戶端還是服務(wù)端,如果一次性進(jìn)行讀取發(fā)送、接收都是不可取,很容易導(dǎo)致內(nèi)存問(wèn)題,下面這篇文章主要給大家介紹了關(guān)于springboot整合vue2-uploader實(shí)現(xiàn)文件分片上傳、秒傳、斷點(diǎn)續(xù)傳功能的相關(guān)資料,需要的朋友可以參考下
    2023-06-06
  • Java Runnable線程傳參,實(shí)現(xiàn)讓run訪問(wèn)參數(shù)

    Java Runnable線程傳參,實(shí)現(xiàn)讓run訪問(wèn)參數(shù)

    這篇文章主要介紹了Java Runnable線程傳參,實(shí)現(xiàn)讓run訪問(wèn)參數(shù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • 劍指Offer之Java算法習(xí)題精講二叉樹(shù)專題篇上

    劍指Offer之Java算法習(xí)題精講二叉樹(shù)專題篇上

    跟著思路走,之后從簡(jiǎn)單題入手,反復(fù)去看,做過(guò)之后可能會(huì)忘記,之后再做一次,記不住就反復(fù)做,反復(fù)尋求思路和規(guī)律,慢慢積累就會(huì)發(fā)現(xiàn)質(zhì)的變化
    2022-03-03

最新評(píng)論