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

SpringBoot 普通類調(diào)用Bean對象的一種方式推薦

 更新時(shí)間:2021年11月16日 09:59:02   作者:morro_c137  
這篇文章主要介紹了SpringBoot 普通類調(diào)用Bean對象的一種方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

SpringBoot 普通類調(diào)用Bean對象

有時(shí)我們有一些特殊的需要,可能要在一個(gè)不被Spring管理的普通類中去調(diào)用Spring管理的bean對象的一些方法,比如一般SpringMVC工程在controller中通過

@Autowired
private TestService testService;

注入TestService 接口就可以調(diào)用此接口實(shí)現(xiàn)類的實(shí)現(xiàn)的方法。

但在一般類中顯然不可以這么做,注入的 TestService 將會報(bào)空指針異常,你無法拿到這個(gè)bean,在一般的ssm工程中我們可以通過xml配置把普通類設(shè)置成一個(gè)bean對象,那么 TestService 就有效了, 或者使用 ApplicationContext 直接讀取xml配置中的bean也可以拿到 TestService。`

Spring Boot 已經(jīng)摒棄了各種繁瑣的xml配置,當(dāng)然就不再使用xml配置的方式,之前在網(wǎng)上看到一種很簡便的方式,但現(xiàn)在又找不到鏈接了,這里做下記錄。

在普通類中定義 ApplicationContext 靜態(tài)變量和set方法

    private static ApplicationContext applicationContext;//啟動(dòng)類set入,調(diào)用下面set方法
    public static void setApplicationContext(ApplicationContext context) {
        applicationContext = context;
    }

在啟動(dòng)類中,啟動(dòng)時(shí)事實(shí)已經(jīng)生成 ConfigurableApplicationContext 對象, ConfigurableApplicationContext 是 ApplicationContext 接口的實(shí)現(xiàn),這里直接傳到普通類的 setApplicationContext 方法就行了

@SpringBootApplication
@ServletComponentScan
public class WxApplication implements EmbeddedServletContainerCustomizer{
    public static void main(String[] args) {
        ConfigurableApplicationContext applicationContext = SpringApplication.run(WxApplication.class, args);
        TestClass.setApplicationContext(applicationContext);
    }
}

由于是靜態(tài)變量,類加載時(shí) applicationContext 已經(jīng)存在,就可獲取到 TestService 了,唯一不好就是靜態(tài)變量在服務(wù)器啟動(dòng)后將一直存在

public class TestClass {
    private static ApplicationContext applicationContext;//啟動(dòng)類set入,調(diào)用下面set方法
    public static void setApplicationContext(ApplicationContext context) {
        // TODO Auto-generated method stub
        applicationContext = context;
    }
    public void getBeanTest(){
        TestService testService  = (TestService)applicationContext.getBean(TestService.class);
    }
}

補(bǔ)充:

在普通 Spring 工程在啟動(dòng)的時(shí)候都會通過 org.springframework.web.context.ContextLoaderListener 監(jiān)聽器從加載系統(tǒng)資源并管理bean, Spring 提供的 WebApplicationContextUtils 工具類能在請求時(shí)獲取到運(yùn)行時(shí)工程的bean,如果看源碼就可以知道監(jiān)聽器執(zhí)行時(shí)與 WebApplicationContextUtils 類的關(guān)聯(lián)

//封裝一下,類的class和請求request為必要參數(shù)
public static <T> T getBean(Class<? extends Object> cla,HttpServletRequest request){
        if(request == null){
            return null;
        }
        return (T)WebApplicationContextUtils.getRequiredWebApplicationContext(request.getServletContext()).getBean(cla);//getBean參數(shù)可為bean類的.class或直接是bean的Id
    }
//這樣獲取bean
TestService testService= (TestService)getBean(TestService.class, request);

SpringBoot 中bean的使用

通常定義bean的方式有三種,注解、xml文件中定義等

但是在采用注解形式定義bean的時(shí)候,如果我們沒有為bean指定名字,那么spring本身也會為bean指定一個(gè)默認(rèn)的名字,名字命名規(guī)則如下:

1、如果類的前兩個(gè)字母都是大寫的話,那么bean的名稱就是類的名稱。比如類的名稱是BEan,那么bean的名稱就是BEan.

2、如果類名只是首字母大寫,那么bean的名稱,就會成為首字母小寫的。比如類的名稱是Bean,那么bean的名稱就是bean

Springboot中bean的使用

很多時(shí)候,我們在開發(fā)spring boot等相關(guān)的spring的web項(xiàng)目時(shí)候,使用bean的方式有幾種:

1、通過autowired字段來引入bean,這樣可以使用bean

2、在程序中,通過spring的ApplicationContext獲取某一個(gè)bean來做操作

  • @Autowired
  • Bean bean;

針對于第一種,我們形式比較常見,通常autowired這種方式引用bean后,直接通過對象名bean就可以調(diào)用對應(yīng)的方法了

針對于第二種,一般就是直接通過bean名稱調(diào)用了

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

相關(guān)文章

最新評論