Springboot 自定義工具類獲取bean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
/** * Created with IntelliJ IDEA. * * @Auther: zp * @Date: 2021/03/26/13:32 * @Description: 通過beanFactory獲取spring管理的bean對象工具類 */ @Component public class ApplicationContextUtil implements ApplicationContextAware { private static ApplicationContext context; // springboot加載完成后會把beanfactory作為參數傳給次方法,然后我們可以把工廠賦值給context。 @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { context = applicationContext; } // 通過context獲取bean public static Object getBean(String beanName) { return context.getBean(beanName); } } |
在工具類注入bean的三種方式
1. 需求/目的
比如:在進行使用HandlerInterceptorAdapter攔截器時,需要訪問數據庫來判斷是否攔截請求,這時就需要在攔截器的判斷類中注入Dao或Service對象來執行sql語句。而直接使用@Autowired無法進行注入。
2.使用環境
spring boot 2.0.3
3.方法一:獲取ApplicationContext上下文
在applicationContext對象中可以獲取到所有的bean
第一步:準備ApplicationContextAware的實現類,用于獲取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
25
26
27
28
29
|
import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springframework.stereotype.Component; import com.authstr.ff.utils.exception.Assert; @Component public class SpringUtils implements ApplicationContextAware { private static Log log = LogFactory.getLog(SpringUtils. class ); private static ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) { SpringUtils.applicationContext = applicationContext; } private static ApplicationContext getContext() { return applicationContext; } public static Object getBean(String beanId) { return SpringUtils.getBean(Object. class , beanId); } public static <T> T getBean(Class<T> clazz, String beanId) throws ClassCastException { ApplicationContext context = SpringUtils.getContext(); Assert.isTrue(StringUtils.hasText(beanId), "beanId must not null!" , true ); boolean a=context.containsBean(beanId); Assert.isTrue(context.containsBean(beanId), "beanId :[" + beanId + "] is not exist!" , true ); Object bean = null ; bean = context.getBean(beanId); return (T)bean; } } |
這是已經寫好的工具類,可以根據bean的id獲取對應的bean
第二步: 對要獲取的bean設置id
如:
1
2
|
@Component ( "basicDaoImpl" ) public class BasicDaoImpl extends AbstractDao implements BasicDao |
第三步: 在要使用的類中寫一個方便調用的方法
1
2
3
|
public BasicDaoImpl getBasicDaoImpl (){ return SpringUtils.getBean(BasicDaoImpl . class , "basicDaoImpl" ); } |
4.方法二:將工具類的對象也添加為bean
第一步:當前類添加@Component注解
第二步:對要獲取的對象使用@Autowired 注解
1
2
|
@Autowired private BasicDaoImpl basicDaoImpl; |
第三步:在創建該工具類的地方,這樣定義
1
2
3
4
|
@Bean public AuthInterceptor authInterceptor(){ return new AuthInterceptor(); } |
5.方法三:在spring Boot 啟動時創建工具類自身的靜態對象
在本質上,同方法二
第一步:當前類添加@Component注解
第二步:在工具類創建一個自身的靜態對象
1
|
public static AuthInterceptor authInterceptor; |
第三步:使用@PostConstruct注解,在springboot加載時執行該方法
1
2
3
4
5
|
@PostConstruct public void init() { authInterceptor= this ; AuthInterceptor .authInterceptor= this .authInterceptor; } |
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/admin123404/article/details/115240279