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"/>
相关推荐
hhzz10 分钟前
Spring Boot整合Activiti的项目中实现抄送功能
java·spring boot·后端
愿你天黑有灯下雨有伞22 分钟前
实战演练:如何在Spring Boot项目中优雅地使用参数校验
spring boot
期待のcode4 小时前
springboot热部署
java·spring boot·后端
Somehow0074 小时前
Spring Boot 集成 ElasticSearch 的简单示例
spring boot·设计
vx_bisheyuange5 小时前
基于SpringBoot的老年一站式服务平台
java·spring boot·后端·毕业设计
计算机毕设VX:Fegn08955 小时前
计算机毕业设计|基于Java + vue水果商城系统(源码+数据库+文档)
java·开发语言·数据库·vue.js·spring boot·课程设计
老华带你飞7 小时前
校务管理|基于springboot 校务管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·spring
JosieBook7 小时前
【部署】Spring Boot + Vue框架项目生产环境部署完整方案
vue.js·spring boot·后端
油丶酸萝卜别吃7 小时前
springboot项目中与接口文档有关的注解
java·spring boot·后端
小码哥0687 小时前
家政服务管理-家政服务管理平台-家政服务管理平台源码-家政服务管理平台java代码-基于springboot的家政服务管理平台
java·开发语言·spring boot·家政服务·家政服务平台·家政服务系统·家政服务管理平台源码