最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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);
 }
}

通過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 沒有使用@Component標(biāo)注,所以在Controller中不能才用注入方式生成CommonClass對(duì)象,而是才用new的方式生成了該對(duì)象。

這樣一來,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ì)象,而是通過工具類的方式

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)文章

最新評(píng)論

清河县| 临泽县| 乌什县| 景泰县| 贵州省| 沙河市| 外汇| 辛集市| 百色市| 梅州市| 中阳县| 谢通门县| 泊头市| 通道| 宜章县| 萝北县| 辛集市| 夹江县| 房产| 梅河口市| 海原县| 桐乡市| 自贡市| 丰镇市| 武义县| 佛山市| 梅州市| 双柏县| 临夏市| 微博| 红河县| 阜康市| 苗栗市| 鄂温| 墨脱县| 沅陵县| 平潭县| 嘉荫县| 财经| 潢川县| 秀山|