栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Springboot实现多线程注入bean的工具类操作

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;



@Component
public class ApplicationContextProvider implements ApplicationContextAware {
  
  private static ApplicationContext applicationContext;

  @Override
  public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
    this.applicationContext = applicationContext;
  }

  
  public static ApplicationContext getApplicationContext() {
    return applicationContext;
  }

  
  public static Object getBean(String name) {
    return getApplicationContext().getBean(name);
  }

  
  public static  T getBean(Class clazz) {
    return getApplicationContext().getBean(clazz);
  }

  
  public static  T getBean(String name, Class 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;
 

@Component
public final class SpringUtils implements BeanFactoryPostProcessor, ApplicationContextAware
{
  
  private static ConfigurableListableBeanFactory beanFactory;
  private static ApplicationContext applicationContext = null;
 
  @Override
  public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException
  {
    SpringUtils.beanFactory = beanFactory;
  }
 
  
  @SuppressWarnings("unchecked")
  public static  T getBean(String name) throws BeansException
  {
    return (T) beanFactory.getBean(name);
  }
 
  
  public static  T getBean(Class clz) throws BeansException
  {
    T result = (T) beanFactory.getBean(clz);
    return result;
  }
 
  
  public static boolean containsBean(String name)
  {
    return beanFactory.containsBean(name);
  }
 
  
  public static boolean isSingleton(String name) throws NoSuchBeanDefinitionException
  {
    return beanFactory.isSingleton(name);
  }
 
  
  public static Class getType(String name) throws NoSuchBeanDefinitionException
  {
    return beanFactory.getType(name);
  }
 
  
  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的工具类操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/132273.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号