Component
public class SpringContextUtils implements ApplicationContextAware {
public static ApplicationContext applicationContext;

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

public static Object getBean(String name) {
return applicationContext.getBean(name);
}

public static <T> T getBean(String name, Class<T> requiredType) {
return applicationContext.getBean(name, requiredType);
}

public static boolean containsBean(String name) {
return applicationContext.containsBean(name);
}

public static boolean isSingleton(String name) {
return applicationContext.isSingleton(name);
}

public static Class<? extends Object> getType(String name) {
return applicationContext.getType(name);
}

}

如果有报ApplicationContext空指针,则可能原因是没加载之前就往下走了,要在要 使用的类 前面加

@DependsOn("springContextUtils")

来源
springboot获取getBean方法以及ApplicationContext空指针问题解决
springboot获取getBean方法以及ApplicationContext空指针问题解决

@PostConstruct +getapplicationcontext.getbean springboot获取getBean的更多相关文章

  1. springboot获取getBean方法以及ApplicationContext空指针问题解决

    创建获取ApplicationContext工具类: package com.performancetest.common.utils; import org.springframework.bean ...

  2. SpringMvc获取getbean

    import org.apache.commons.lang3.Validate; import org.slf4j.Logger; import org.slf4j.LoggerFactory; i ...

  3. springboot 获取控制器参数的几种方式

    这里介绍springboot 获取控制器参数有四种方式 1.无注解下获取参数 2.使用@RequestParam获取参数 3.传递数组 4.通过URL传递参数 无注解下获取参数无注解下获取参数,需要控 ...

  4. springboot获取项目的绝对路径和根目录

    springboot获取当前项目路径的地址 System.getProperty("user.dir") 输出目录:  G:\outshine\wangsoso //获取class ...

  5. SpringBoot获取http请求参数的方法

    SpringBoot获取http请求参数的方法 原文:https://www.cnblogs.com/zhanglijun/p/9403483.html 有七种Java后台获取前端传来参数的方法,稍微 ...

  6. SpringBoot获取配置文件,就这么简单。

    在讲SpringBoot 获取配置文件之前我们需要对SpringBoot 的项目有一个整体的了解,如何创建SpringBoot 项目,项目结构等等知识点,我在这里就不一一讲述了,没有学过的小伙伴可以自 ...

  7. Spring boot获取getBean

    package com.job.center.quartz.common; import org.springframework.beans.BeansException; import org.sp ...

  8. springboot获取applicationcontext

    使用springboot之前,我们通过ClassPathXmlApplicationContext加载spring xml配置文件来获取applicationcontext,使用springboot后 ...

  9. springboot中spring.profiles.active来引入多个properties文件 & Springboot获取容器中对象

    1.    引入多个properties文件 很多时候,我们项目在开发环境和生成环境的环境配置是不一样的,例如,数据库配置,在开发的时候,我们一般用测试数据库,而在生产环境的时候,我们是用正式的数据, ...

随机推荐

  1. 【论文笔记】A review of applications in federated learning(综述)

    A review of applications in federated learning Authors Li Li, Yuxi Fan, Mike Tse, Kuo-Yi Lin Keyword ...

  2. 按照 Promise/A+ 规范逐行注释并实现 Promise

    0. 前言 面试官:「你写个 Promise 吧.」 我:「对不起,打扰了,再见!」 现在前端越来越卷,不会手写 Promise 都不好意思面试了(手动狗头.jpg).虽然没多少人会在业务中用自己实现 ...

  3. mybatis各阶段的详解

    1 本阶段的需要注意的几个点 1,首先是在核心配置文件里面的内容: 配置的顺序,不配则不用管,配则必须按顺序来!!!! properties?, settings?, typeAliases?, ty ...

  4. nginx1.1 nginx介绍和反向代理

    1.什么是nginx nginx是一个高性能的http和反向代理的web服务器,所占内存小,高并发 nginx默认端口:80端口 命令存放目录:cd /usr/local/nginx/sbin 配置文 ...

  5. brew常用命令

    Homebrew 常用命令 brew -help # 查看帮助命令 brew config # 查看配置信息 brew list # 查看已安装软件包列表 brew cleanup # 清理所有包的旧 ...

  6. python之re模块补充和其他模块(collection、time、queue、datetime、random)

    目录 re模块补充说明 collections模块 queue模块 time模块 datetime模块 random模块 re模块补充说明 在正则表达式中,'()'的作用是进行分组,但是在re模块中, ...

  7. CoaXPress 是如何只用一条线缆实现双向传输和供电的

    这是个很有意思的事情,CoaXPress的全双工双向数据传输.且供电只需要一条同轴线缆,这个原理对其它串行接口的设计是非常有参考价值的,尤其是对线缆长度.数量有严格要求的场合,一条同轴线缆走天下,不要 ...

  8. TKE qGPU 通过 CRD 管理集群 GPU 卡资源

    作者 刘旭,腾讯云高级工程师,专注容器云原生领域,有多年大规模 Kubernetes 集群管理经验,现负责腾讯云 GPU 容器的研发工作. 背景 目前 TKE 已提供基于 qGPU 的算力/显存强隔离 ...

  9. Kafka 消费者解析

    一.消费者相关概念 1.1 消费组&消费者 消费者: 消费者从订阅的主题消费消息,消费消息的偏移量保存在Kafka的名字是__consumer_offsets的主题中 消费者还可以将⾃⼰的偏移 ...

  10. CSS中html的标签元素分类

    在CSS中,html中的标签元素大体被分为三种不同的类型: 块状元素.内联元素(又叫行内元素)和内联块状元素.    常用的块状元素有:  <div>.<p>.<h1&g ...