当前位置: 移动技术网 > IT编程>开发语言>Java > Springboot实现多线程注入bean的工具类操作

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

2020年08月27日  | 移动技术网IT编程  | 我要评论
场景: 使用springboot多线程,线程类无法自动注入需要的bean解决方法: 通过工具类获取需要的bean工具类代码:import org.springframework.beans.beans

场景: 使用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的工具类操作就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持移动技术网。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网