1. 程式人生 > 程式設計 >Springboot實現多執行緒注入bean的工具類操作

Springboot實現多執行緒注入bean的工具類操作

場景: 使用springboot多執行緒,執行緒類無法自動注入需要的bean

解決方法: 通過工具類獲取需要的bean

工具類程式碼:

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;

/**
 * @Description: 獲取bean物件的工具類
 * @Author: Zhang Lin
 * @CreateDate: 2018/12/10
 */

@Component
public class ApplicationContextProvider implements ApplicationContextAware {
  /**
   * 上下文物件例項
   */
  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);
  }
}

使用方法:

線上程類的建構函式裡呼叫工具類的getBeans方法獲取例項,如:

public class ThreadA implements Runnable {
  private Service service;
  public ThreadA() {
    this.service = ApplicationContextProvider.getBean(Service.class);
  }

  @Override
  public void run() {
  //TO BE DONE
  }
}

補充知識:在springboot中普通的執行緒類訪問service類

1、首先線上程類上註解@Component

2、@Autowired

private IStudentService studentService;

3、呼叫時候

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管理環境中獲取bean
 * 
 * @author ruoyi
 */
@Component
public final class SpringUtils implements BeanFactoryPostProcessor,ApplicationContextAware
{
  /** Spring應用上下文環境 */
  private static ConfigurableListableBeanFactory beanFactory;
  private static ApplicationContext applicationContext = null;
 
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
  {
    SpringUtils.beanFactory = beanFactory;
  }
 
  /**
   * 獲取物件
   *
   * @param name
   * @return Object 一個以所給名字註冊的bean的例項
   * @throws org.springframework.beans.BeansException
   *
   */
  @SuppressWarnings("unchecked")
  public static <T> T getBean(String name) throws BeansException
  {
    return (T) beanFactory.getBean(name);
  }
 
  /**
   * 獲取型別為requiredType的物件
   *
   * @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包含一個與所給名稱匹配的bean定義,則返回true
   *
   * @param name
   * @return boolean
   */
  public static boolean containsBean(String name)
  {
    return beanFactory.containsBean(name);
  }
 
  /**
   * 判斷以給定名字註冊的bean定義是一個singleton還是一個prototype。 如果與給定名字相應的bean定義沒有被找到,將會丟擲一個異常(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 註冊物件的型別
   * @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實現多執行緒注入bean的工具類操作就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。