国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - 詳解SpringBoot靜態方法獲取bean的三種方式

詳解SpringBoot靜態方法獲取bean的三種方式

2022-02-25 00:39chilx Java教程

本文主要介紹了詳解SpringBoot靜態方法獲取bean的三種方式,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

方式一  注解@PostConstruct

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import com.example.javautilsproject.service.AutoMethodDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
 
import javax.annotation.PostConstruct;
 
/**
 * springboot靜態方法獲取 bean 的三種方式(一)
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@Component
public class StaticMethodGetBean_1 {
 
    @Autowired
    private AutoMethodDemoService autoMethodDemoService;
 
    @Autowired
    private static AutoMethodDemoService staticAutoMethodDemoService;
 
    @PostConstruct
    public void init() {
        staticAutoMethodDemoService = autoMethodDemoService;
    }
 
    public static String getAuthorizer() {
        return staticAutoMethodDemoService.test();
    }
}

注解@PostConstruct說明

PostConstruct 注釋用于在依賴關系注入完成之后需要執行的方法上,以執行任何初始化。此方法必須在將類放入服務之前調用。支持依賴關系注入的所有類都必須支持此注釋。即使類沒有請求注入任何資源,用 PostConstruct 注釋的方法也必須被調用。只有一個方法可以用此注釋進行注釋。

應用 PostConstruct 注釋的方法必須遵守以下所有標準:

  • 該方法不得有任何參數,除非是在 EJB 攔截器 (interceptor) 的情況下,根據 EJB 規范的定義,在這種情況下它將帶有一個 InvocationContext 對象 ;
  • 該方法的返回類型必須為 void;
  • 該方法不得拋出已檢查異常;
  • 應用 PostConstruct 的方法可以是 public、protected、package private 或 private;
  • 除了應用程序客戶端之外,該方法不能是 static;
  • 該方法可以是 final;
  • 如果該方法拋出未檢查異常,那么不得將類放入服務中,除非是能夠處理異常并可從中恢復的 EJB。

方式二  啟動類ApplicationContext

實現方式:在springboot的啟動類中,定義static變量ApplicationContext,利用容器的getBean方法獲得依賴對象

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;
/**
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@SpringBootApplication
public class Application {
    public static ConfigurableApplicationContext ac;
    public static void main(String[] args) {
       ac = SpringApplication.run(Application.class, args);
    }
}

調用方式

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/**
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@RestController
public class TestController {
    /**
     * 方式二
     */
    @GetMapping("test2")
    public void method_2() {
        AutoMethodDemoService methodDemoService = Application.ac.getBean(AutoMethodDemoService.class);
        String test2 = methodDemoService.test2();
        System.out.println(test2);
    }
}

方式三 手動注入ApplicationContext

手動注入ApplicationContext

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
 
 
/**
 * springboot靜態方法獲取 bean 的三種方式(三)
 * @author: clx
 * @date: 2019/7/23
 * @version: 1.1.0
 */
@Component
public class StaticMethodGetBean_3<T> implements ApplicationContextAware {
    private static ApplicationContext applicationContext;
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        StaticMethodGetBean_3.applicationContext = applicationContext;
    }
 
    public static <T> T  getBean(Class<T> clazz) {
        return applicationContext != null?applicationContext.getBean(clazz):null;
    }
}

調用方式

?
1
2
3
4
5
6
7
8
9
/**
 * 方式三
 */
@Test
public void method_3() {
    AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
    String test3 = autoMethodDemoService.test3();
    System.out.println(test3);
}

以上三種方式樓主都測試過可以為完美使用

到此這篇關于詳解SpringBoot靜態方法獲取bean的三種方式的文章就介紹到這了,更多相關SpringBoot靜態方法獲取bean內容請搜索服務器之家以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持服務器之家!

原文鏈接:https://blog.csdn.net/showchi/article/details/97005720

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 日韩av免费播放 | 久久国产精品视频 | 日本在线观看 | 亚洲国产精品一区二区久久 | 国产三级一区 | 午夜私人影院 | 视频1区 | 国产免费久久精品 | 日韩三区视频 | av亚洲在线 | 中文字幕日韩在线 | a毛片| 亚洲成人精品一区 | 成人免费毛片在线观看 | 激情综合网激情 | 午夜影院免费观看 | 综合伊人 | 91精选视频在线观看 | 精品国产三级 | 中文字幕一区二区三区在线视频 | 精品一区二区视频 | av一区二区不卡 | eeuss国产一区二区三区四区 | 亚洲 欧美 日韩在线 | 国产a级大片| 成人福利在线观看 | 在线播放国产精品 | 中文字幕日韩欧美一区二区三区 | 成年人免费观看网站 | 亚洲国产精品99久久久久久久久 | 亚洲免费视频大全 | 欧美日本韩国一区二区三区 | 亚洲视频在线免费观看 | 久久久久久久久久久久99 | 国产精品中文字幕在线 | 99热热热热| 亚洲国产成人精品久久久国产成人一区 | 日韩午夜电影 | 日韩中文字幕 | 婷婷五月色综合香五月 | 在线播放亚洲 |