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"/>
相关推荐
Java程序员-小白1 小时前
Spring Boot整合Sa-Token框架(入门篇)
java·spring boot·后端·sa-token
小楊不秃头1 小时前
SpringBoot: IoC&DI
spring boot·ioc·di
绝知此事2 小时前
ELK 从入门到精通:Spring Boot 实战三部曲(三)—— 高级应用与架构设计
spring boot·后端·elk
Devin~Y2 小时前
从内容社区到AIGC客服:Spring Boot、Redis、Kafka、K8s、RAG的三轮大厂Java面试对话(附标准答案)
java·spring boot·redis·spring cloud·kafka·kubernetes·micrometer
心之伊始3 小时前
Spring Boot 接入 MCP 实战:用 Spring AI 调用本地工具的最小闭环
java·spring boot·agent·spring ai·mcp
西凉的悲伤3 小时前
Spring Boot 、Spring Cloud 微服务架构认证授权方案
spring boot·spring cloud·微服务·架构·认证授权
砍材农夫3 小时前
物联网实战:Spring Boot + Netty 搭建 MQTT | MQTT 设备模拟器
java·spring boot·后端·物联网·struts·spring·netty
小同志004 小时前
application.properties 和 application.yml
java·spring boot·spring·application.yml·.properities
唐青枫4 小时前
Java JdbcTemplate 实战指南:用 Spring 轻量完成数据库增删改查
java·spring boot·spring