Springboot實(shí)現(xiàn)多線程注入bean的工具類操作
場(chǎng)景: 使用springboot多線程,線程類無法自動(dòng)注入需要的bean
解決方法: 通過工具類獲取需要的bean
工具類代碼:
import org.springframework.beans.BeansException;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component;/** * @Description: 獲取bean對(duì)象的工具類 * @Author: Zhang Lin * @CreateDate: 2018/12/10 */@Componentpublic class ApplicationContextProvider implements ApplicationContextAware { /** * 上下文對(duì)象實(shí)例 */ private static ApplicationContext applicationContext; @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } /** * 獲取applicationContext * * @return */ public static ApplicationContext getApplicationContext() { return applicationContext; } /** * 通過name獲取 Bean. * * @param name * @return */ public static Object getBean(String name) { return getApplicationContext().getBean(name); } /** * 通過class獲取Bean. * * @param clazz * @param <T> * @return */ public static <T> T getBean(Class<T> clazz) { return getApplicationContext().getBean(clazz); } /** * 通過name,以及Clazz返回指定的Bean * * @param name * @param clazz * @param <T> * @return */ public static <T> T getBean(String name, Class<T> clazz) { return getApplicationContext().getBean(name, clazz); }}
使用方法:
在線程類的構(gòu)造函數(shù)里調(diào)用工具類的getBeans方法獲取實(shí)例,如:
public class ThreadA implements Runnable { private Service service; public ThreadA() { this.service = ApplicationContextProvider.getBean(Service.class); } @Override public void run() { //TO BE DONE }}
補(bǔ)充知識(shí):在springboot中普通的線程類訪問service類
1、首先在線程類上注解@Component
2、@Autowired
private IStudentService studentService;
3、調(diào)用時(shí)候
studentService = SpringUtils.getBean('studentService');
4、SpringUtils
package com.ruoyi.common.utils.spring; import org.springframework.beans.BeansException;import org.springframework.beans.factory.NoSuchBeanDefinitionException;import org.springframework.beans.factory.config.BeanFactoryPostProcessor;import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;import org.springframework.context.ApplicationContext;import org.springframework.context.ApplicationContextAware;import org.springframework.stereotype.Component; /** * spring工具類 方便在非spring管理環(huán)境中獲取bean * * @author ruoyi */@Componentpublic final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware{ /** Spring應(yīng)用上下文環(huán)境 */ private static ConfigurableListableBeanFactory beanFactory; private static ApplicationContext applicationContext = null; @Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { SpringUtils.beanFactory = beanFactory; } /** * 獲取對(duì)象 * * @param name * @return Object 一個(gè)以所給名字注冊(cè)的bean的實(shí)例 * @throws org.springframework.beans.BeansException * */ @SuppressWarnings('unchecked') public static <T> T getBean(String name) throws BeansException { return (T) beanFactory.getBean(name); } /** * 獲取類型為requiredType的對(duì)象 * * @param clz * @return * @throws org.springframework.beans.BeansException * */ public static <T> T getBean(Class<T> clz) throws BeansException { T result = (T) beanFactory.getBean(clz); return result; } /** * 如果BeanFactory包含一個(gè)與所給名稱匹配的bean定義,則返回true * * @param name * @return boolean */ public static boolean containsBean(String name) { return beanFactory.containsBean(name); } /** * 判斷以給定名字注冊(cè)的bean定義是一個(gè)singleton還是一個(gè)prototype。 如果與給定名字相應(yīng)的bean定義沒有被找到,將會(huì)拋出一個(gè)異常(NoSuchBeanDefinitionException) * * @param name * @return boolean * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException { return beanFactory.isSingleton(name); } /** * @param name * @return Class 注冊(cè)對(duì)象的類型 * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static Class<?> getType(String name) throws NoSuchBeanDefinitionException { return beanFactory.getType(name); } /** * 如果給定的bean名字在bean定義中有別名,則返回這些別名 * * @param name * @return * @throws org.springframework.beans.factory.NoSuchBeanDefinitionException * */ public static String[] getAliases(String name) throws NoSuchBeanDefinitionException { return beanFactory.getAliases(name); } @Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { if(SpringUtils.applicationContext == null){ SpringUtils.applicationContext = applicationContext; } } //獲取applicationContext public static ApplicationContext getApplicationContext() { return applicationContext; } }
以上這篇Springboot實(shí)現(xiàn)多線程注入bean的工具類操作就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. python爬蟲實(shí)戰(zhàn)之制作屬于自己的一個(gè)IP代理模塊2. Java程序的編碼規(guī)范(6)3. HTML 絕對(duì)路徑與相對(duì)路徑概念詳細(xì)4. python 利用toapi庫自動(dòng)生成api5. Spring如何使用xml創(chuàng)建bean對(duì)象6. Android Studio設(shè)置顏色拾色器工具Color Picker教程7. python實(shí)現(xiàn)在內(nèi)存中讀寫str和二進(jìn)制數(shù)據(jù)代碼8. IntelliJ IDEA設(shè)置默認(rèn)瀏覽器的方法9. python實(shí)現(xiàn)PolynomialFeatures多項(xiàng)式的方法10. python實(shí)現(xiàn)讀取類別頻數(shù)數(shù)據(jù)畫水平條形圖案例
