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. [cf]Codeforces Round #784(Div 4)

    由于一次比赛被虐得太惨,,生发开始写blog的想法,于是便有了这篇随笔(找了个近期的cf比赛练练手(bushi))第一次写blog,多多包涵. 第二场cf比赛,第一场打的Div2,被虐太惨,所以第二场 ...

  2. [AcWing 28] 在O(1)时间删除链表结点

    点击查看代码 /** * Definition for singly-linked list. * struct ListNode { * int val; * ListNode *next; * L ...

  3. GO语言学习——切片二

    使用make()函数构造切片 格式: make([]T, size, cap) 其中: T:切片的元素类型 size:切片中元素的数量 cap:切片的容量 切片的本质 切片的本质就是对底层数组的封装, ...

  4. 【LINT】cpplint修改版:自定义编码风格检查工具lint

    github:https://github.com/skullboyer/code-check Code Check 本仓介绍的内容涉及代码静态检查和编码风格检查 但主要放在编码风格检查,lint是基 ...

  5. nacos 详细介绍(二)

    五.nacos的namespace和group namespace:相当于环境,开发环境 测试环境 生产环境 ,每个空间里面的配置是独立的默认的namespace是public, namespace可 ...

  6. zabbix-agent python脚本侦听服务器异常登录,并告警

    py脚本 import re,subprocess,time,datetime #gpasswd -a zabbix adm def ftime(a): a = a.replace('Jan','01 ...

  7. 【多线程】线程休眠 Thread.sleep()

    线程休眠 Thread.sleep() sleep (时间) 指定当前线程阻塞的毫秒数: sleep存在异常InterruptedException: sleep时间达到后线程进入就绪状态: slee ...

  8. Python的.gitignore模板

    参考:https://github.com/github/gitignore Python的.gitignore模板,记录一下方便查询 # Byte-compiled / optimized / DL ...

  9. 811. Subdomain Visit Count - LeetCode

    Question 811. Subdomain Visit Count Example 1: Input: ["9001 discuss.leetcode.com"] Output ...

  10. 806. Number of Lines To Write String - LeetCode

    Question 806. Number of Lines To Write String Solution 思路:注意一点,如果a长度为4,当前行已经用了98个单元,要另起一行. Java实现: p ...