SpringBoot线程无法通过@Autowired注入Bean

html 复制代码
package com.wis.mes.context;

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

/**
 * @Author CHENEY
 * @Date 2019/03/26
 * @Version 1.0
 * @Last Modified By : CHENEY
 * @Last Modified Time : 2019/03/26
 * @Description :  bean对象的工具类 (ApplicationContextProvider Service)
 * @function:针对多线程无法使用Autowired注入Bean设计
 * @Type implements class
 * Copyright (c) 2019 WIS Software Co.*
 */
@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);
    }
}

-------------------------------------------------------- 用法:

html 复制代码
DataCoreService dataCoreService = ApplicationContextProvider.getBean(DataCoreService.class);
复制代码
注意这里:

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

2、web.xml 添加监听

<listener>
    <description>spring监听器</description>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
 
如果启动时报找不到 applicationContext.xml 添加以下语句
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
</context-param>
2、applicationContext.xml 添加以下语句

<bean class="com.zzf.base.ApplicationContextProvider"  lazy-init="false"/>
相关推荐
李白的粉5 小时前
基于springboot的在线教育系统
java·spring boot·毕业设计·课程设计·在线教育系统·源代码
小马爱打代码6 小时前
SpringBoot原生实现分布式MapReduce计算
spring boot·分布式·mapreduce
iuyou️6 小时前
Spring Boot知识点详解
java·spring boot·后端
一弓虽6 小时前
SpringBoot 学习
java·spring boot·后端·学习
来自星星的猫教授8 小时前
spring,spring boot, spring cloud三者区别
spring boot·spring·spring cloud
乌夷9 小时前
使用spring boot vue 上传mp4转码为dash并播放
vue.js·spring boot·dash
A阳俊yi11 小时前
Spring Boot日志配置
java·spring boot·后端
苹果酱056711 小时前
2020-06-23 暑期学习日更计划(机器学习入门之路(资源汇总)+概率论)
java·vue.js·spring boot·mysql·课程设计
斜月11 小时前
一个服务预约系统该如何设计?
spring boot·后端
Java水解12 小时前
线程池详解:在SpringBoot中的最佳实践
spring boot·后端