詳解SpringBoot靜態(tài)方法獲取bean的三種方式
方式一 注解@PostConstruct
import com.example.javautilsproject.service.AutoMethodDemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
/**
* springboot靜態(tài)方法獲取 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說(shuō)明
PostConstruct 注釋用于在依賴(lài)關(guān)系注入完成之后需要執(zhí)行的方法上,以執(zhí)行任何初始化。此方法必須在將類(lèi)放入服務(wù)之前調(diào)用。支持依賴(lài)關(guān)系注入的所有類(lèi)都必須支持此注釋。即使類(lèi)沒(méi)有請(qǐng)求注入任何資源,用 PostConstruct 注釋的方法也必須被調(diào)用。只有一個(gè)方法可以用此注釋進(jìn)行注釋。
應(yīng)用 PostConstruct 注釋的方法必須遵守以下所有標(biāo)準(zhǔn):
- 該方法不得有任何參數(shù),除非是在 EJB 攔截器 (interceptor) 的情況下,根據(jù) EJB 規(guī)范的定義,在這種情況下它將帶有一個(gè) InvocationContext 對(duì)象 ;
- 該方法的返回類(lèi)型必須為 void;
- 該方法不得拋出已檢查異常;
- 應(yīng)用 PostConstruct 的方法可以是 public、protected、package private 或 private;
- 除了應(yīng)用程序客戶(hù)端之外,該方法不能是 static;
- 該方法可以是 final;
- 如果該方法拋出未檢查異常,那么不得將類(lèi)放入服務(wù)中,除非是能夠處理異常并可從中恢復(fù)的 EJB。
方式二 啟動(dòng)類(lèi)ApplicationContext
實(shí)現(xiàn)方式:在springboot的啟動(dòng)類(lèi)中,定義static變量ApplicationContext,利用容器的getBean方法獲得依賴(lài)對(duì)象
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);
}
}
調(diào)用方式
/**
* @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);
}
}
方式三 手動(dòng)注入ApplicationContext
手動(dòng)注入ApplicationContext
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* springboot靜態(tài)方法獲取 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;
}
}
調(diào)用方式
/**
* 方式三
*/
@Test
public void method_3() {
AutoMethodDemoService autoMethodDemoService = StaticMethodGetBean_3.getBean(AutoMethodDemoService.class);
String test3 = autoMethodDemoService.test3();
System.out.println(test3);
}
以上三種方式樓主都測(cè)試過(guò)可以為完美使用
到此這篇關(guān)于詳解SpringBoot靜態(tài)方法獲取bean的三種方式的文章就介紹到這了,更多相關(guān)SpringBoot靜態(tài)方法獲取bean內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring Boot設(shè)置支持跨域請(qǐng)求過(guò)程詳解
這篇文章主要介紹了Spring Boot設(shè)置支持跨域請(qǐng)求過(guò)程詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
SpringBoot 請(qǐng)求參數(shù)忽略大小寫(xiě)的實(shí)例
這篇文章主要介紹了SpringBoot 請(qǐng)求參數(shù)忽略大小寫(xiě)的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01
SpringBoot使用classfinal-maven-plugin插件加密Jar包的示例代碼
這篇文章給大家介紹了SpringBoot使用classfinal-maven-plugin插件加密Jar包的實(shí)例,文中通過(guò)代碼示例和圖文講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2024-02-02
Java?延時(shí)隊(duì)列及簡(jiǎn)單使用方式詳解
這篇文章主要介紹了Java延時(shí)隊(duì)列簡(jiǎn)單使用方式,通過(guò)本文學(xué)習(xí)知道延時(shí)隊(duì)列是什么可以用來(lái)干什么,本文通過(guò)實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-08-08
SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法
本篇文章主要介紹了SpringBoot 實(shí)戰(zhàn) 之 優(yōu)雅終止服務(wù)的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-05-05
J2SE基礎(chǔ)之命令行中編寫(xiě)第一個(gè) Hello World
“Hello World”程序指的是只在計(jì)算機(jī)屏幕上輸出“Hello, World!”(意為“世界,你好!”)這行字符串的計(jì)算機(jī)程序。hello world作為所有編程語(yǔ)言的起始階段,占據(jù)著無(wú)法改變的地位,所有的編程第一步就在于此了!經(jīng)典之中的經(jīng)典!hello world!2016-05-05
java項(xiàng)目中的絕對(duì)路徑和相對(duì)路徑用法說(shuō)明
這篇文章主要介紹了java項(xiàng)目中的絕對(duì)路徑和相對(duì)路徑用法說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-08-08
SpringBoot集成RocketMQ實(shí)現(xiàn)消息發(fā)送的三種方式
RocketMQ?支持3?種消息發(fā)送方式:?同步?(sync)、異步(async)、單向(oneway),本文就將給大家介紹一下SpringBoot集成RocketMQ實(shí)現(xiàn)消息發(fā)送的三種方式文中有詳細(xì)的代碼示例,需要的朋友可以參考下2023-09-09
java?stream使用指南之sorted使用及進(jìn)階方式
這篇文章主要介紹了java?stream使用指南之sorted使用及進(jìn)階方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-05-05

