SpringBoot如何在线程中获取@Service Bean类

目录
  • 如何在线程中获取@ServiceBean类
    • 我们现在开始解决问题
  • 多线程中获取bean对象
    • 创建类
    • 多线程中的调用方式

如何在线程中获取@Service Bean类

这个适用于没有Spring配置文件的Springboot项目中,有配置文件的话取bean就方便多了。

下图是我用@Service注解声明的一个Mybatis Mapper Bean,平常在Springboot扫描配置下的类直接用

@Autowired注解依赖注入。

我现在需要在线程中使用,然而Springboot自然而然只能扫描到自己的东西 ,线程实现的Runnable接口,

我们现在开始解决问题

1、首先创建一个配置类继承ApplicationContextAware,取得ApplicationContext。

利用里面的getBean方法取得你想要的Bean类。

这样你就能在线程中得到你要的Bean类了。挺坑的!!!

多线程中获取bean对象

注:多线程场景下,使用默认的spring自动装配无法获取bean对象,此方案可以从context上下文中直接获取bean。

创建类

实现ApplicationContextAware接口;

package com.bond.match.utils;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
 * Created with IntelliJ IDEA.
 * Date: 2018/1/11 0011
 * Time: 13:20
 * To change this template use File | Settings | File Templates.
 */
@Component
public class ApplicationContextProvider implements ApplicationContextAware {
    private static ApplicationContext context;
    private ApplicationContextProvider(){}
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        context = applicationContext;
    }
    public  static <T> T getBean(Class<T> aClass){
        return context.getBean(aClass);
    }
}

多线程中的调用方式

.method()是bean对象的方法名称

ApplicationContextProvider.getBean(AccountAssetService.class).method()

以上为个人经验,希望能给大家一个参考,也希望大家多多支持我们。

(0)

相关推荐

  • 在spring boot中使用java线程池ExecutorService的讲解

    1. 认识java线程池 1.1 在什么情况下使用线程池? 1.单个任务处理的时间比较短 2.需处理的任务的数量大 1.2 使用线程池的好处: 1.减少在创建和销毁线程上所花的时间以及系统资源的开销 2.如不使用线程池,有可能造成系统创建大量线程而导致消耗完系统内存 1.3 线程池包括以下四个基本组成部分: 1.线程池管理器(ThreadPool):用于创建并管理线程池,包括 创建线程池,销毁线程池,添加新任务: 2.工作线程(PoolWorker):线程池中线程,在没有任务时处于等待状态,可以

  • 详解SpringBoot 多线程处理任务 无法@Autowired注入bean问题解决

    在多线程处理问题时,无法通过@Autowired注入bean,报空指针异常, 在线程中为了线程安全,是防注入的,如果要用到这个类,只能从bean工厂里拿个实例. 解决方法如下: 1.创建一个工具类代码: package com.hqgd.pms.common; import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.spri

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

    场景: 使用springboot多线程,线程类无法自动注入需要的bean 解决方法: 通过工具类获取需要的bean 工具类代码: import org.springframework.beans.BeansException; import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContextAware; import org.springfram

  • SpringBoot如何在线程中获取@Service Bean类

    目录 如何在线程中获取@ServiceBean类 我们现在开始解决问题 多线程中获取bean对象 创建类 多线程中的调用方式 如何在线程中获取@Service Bean类 这个适用于没有Spring配置文件的Springboot项目中,有配置文件的话取bean就方便多了. 下图是我用@Service注解声明的一个Mybatis Mapper Bean,平常在Springboot扫描配置下的类直接用 @Autowired注解依赖注入. 我现在需要在线程中使用,然而Springboot自然而然只能扫

  • SpringBoot 如何从容器中获取对象

    目录 如何从容器中获取对象 SpringBoot中的容器 容器功能 1.组件添加 2.原生配置文件引入(xml文件引入) 3.配置绑定 如何从容器中获取对象 有时候在项目中,我们会自己创建一些类,类中需要使用到容器中的一些类.方法是新建类并实现ApplicationContextAware 接口,在类中建立静态对象 ApplicationContext 对象,这个对象就如同xml配置中的 applicationContext.xml,容器中类都可以获取到. 例如@Service. @Compon

  • ASP.NET在底层类库中获取Session C#类中获取Session 原创

    类库中获取Session首先要添加引用 获取Session 复制代码 代码如下: string user = (string)HttpContext.Current.Session["user"]; 获取Page 复制代码 代码如下: System.Web.UI.Page page = (System.Web.UI.Page)HttpContext.Current.Handler; 获取当前 Request Response 等对象都是在这里 复制代码 代码如下: HttpRespon

  • springboot 正确的在异步线程中使用request的示例代码

    目录 起因: 发现有人踩过坑,但是没解决 尝试寻找官方支持 尝试自己解决 还是甩给官方 解决 结论 起因: 有后端同事反馈在异步线程中获取了request中的参数,然后下一个请求是get请求的话,发现会偶尔出现参数丢失的问题. 示例代码: @GetMapping("/getParams") public String getParams(String a, int b) { return "get success"; } @PostMapping("/po

  • Android 在子线程中更新UI的几种方法示例

    本文介绍了Android 在子线程中更新UI的几种方法示例,分享给大家,具体如下: 方式一:Handler和Message ① 实例化一个Handler并重写handlerMessage()方法 private Handler handler = newHandler() { public void handleMessage(Message msg) { // 处理消息 super.handleMessage(msg); switch (msg.what) { case 1: button1.

  • k8s 中的 service 如何找到绑定的 Pod 及实现 Pod 负载均衡的方法

    目录 k8s 中的 service 如何找到绑定的 Pod 以及如何实现 Pod 负载均衡 前言 endpoint kube-proxy userspace 模式 iptables ipvs kernelspace 服务发现 环境变量 DNS 总结 参考 k8s 中的 service 如何找到绑定的 Pod 以及如何实现 Pod 负载均衡 前言 Service 资源主要用于为 Pod 对象提供一个固定.统一的访问接口及负载均衡的能力. service 是一组具有相同 label pod 集合的抽

  • 关于SpringBoot获取IOC容器中注入的Bean(推荐)

    一: 注入一个TestUtils类 package com.shop.sell.Utils; import com.shop.sell.dto.CartDTO; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class TestUtils { @Bean(name="test

  • SpringBoot中获取profile的方法详解

    目录 spring boot与profile 静态获取方式 autowire ProfileConfig spring boot与profile spring boot 的项目中不再使用xml的方式进行配置,并且,它还遵循着约定大于配置. 静态获取方式 静态工具类获取当前项目的profile环境. import org.springframework.beans.BeansException; import org.springframework.context.ApplicationConte

  • SpringBoot在自定义类中调用service层等Spring其他层操作

    背景: 做了一个TCP服务器来接入智能设备,然后需要将设备实时发送的定位等关键信息存储到数据库. 为了考虑将来可能对外提供rest接口,采用将TCP服务器集成到SpringBoot框架,当然,也是为了能最快利用mybatis框架实现数据访问,然后依次解决了如何启动,如何注销等各种问题,然后在TCP服务器消息处理时,需要写数据库,直接调用DAO层,编译报错. 改为调用Service层,编译正常,运行到调用的地方,报空指针异常,跟踪到异常位置,发现service为空,也就是按照之前controlle

  • Spring在代码中获取bean的几种方式详解

    方法如下 方法一:通过读取XML文件反射生成对象 方法二:通过Spring提供的utils类获取ApplicationContext对象 方法三:继承自抽象类ApplicationObjectSupport 方法四:继承自抽象类WebApplicationObjectSupport 方法五:实现接口ApplicationContextAware 方法六:通过Spring提供的ContextLoader 获取spring中bean的方式总结: 方法一:通过读取XML文件反射生成对象 Applica

随机推荐