创建获取ApplicationContext工具类:

 package com.performancetest.common.utils;

 import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; import java.util.Map; /**
* Spring Context 工具类
*/
@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空指针问题解决的更多相关文章

  1. @PostConstruct +getapplicationcontext.getbean springboot获取getBean

    Componentpublic class SpringContextUtils implements ApplicationContextAware { public static Applicat ...

  2. ApplicationContext之getBean方法详解

    转自:http://www.sohu.com/a/115194552_466964 我们知道可以通过ApplicationContext的getBean方法来获取Spring容器中已初始化的bean. ...

  3. 踩坑了!使用 @Autowired 注入成功,GetBean 方法却获取不到?!

    本文首发于个人微信公众号:Coder小黑 踩坑了?! 之前推文已经讲过 当@Transactional遇到@CacheEvict,你的代码是不是有bug! 现在要在事务提交之后清除缓存.在Spring ...

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

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

  5. 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值

    7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...

  6. Spring:使用Spring AOP时,如何获取目标方法上的注解

    当使用spring AOP时,判断目标方法上的注解进行相关操作,如缓存,认证权限等 自定义注解 package com.agent.annotation; import java.lang.annot ...

  7. 模拟Spring中的getBean方法

    一直知道Spring是运用反射技术的,但具体怎么用呢?今天就模拟下getBean方法. 步骤: 1.用Dom4j解析xml配置文件,取出我们需要的信息 2.遍历Bean节点,根据每个Bean节点的cl ...

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

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

  9. Springboot消除switch-case方法

    Springboot消除switch-case方法 背景 最近,在使用springboot开发一个接口的时候,需要根据接收的请求事件类型,去执行不同的操作,返回不同的结果,基本逻辑如下: String ...

随机推荐

  1. 剑指offer15 链表中倒数第k个结点

    错误代码 class Solution { public: ListNode* FindKthToTail(ListNode* pListHead, unsigned int k) { ) retur ...

  2. CornerStone使用跳坑总结

    Cornerstone是专门为Mac用户设计的Subversion(SVN)的控制,使版本控制更加透明.cornerstone根Xcode相比,能够更好的忽略文件,所以除了项目经理第一次初始化项目的时 ...

  3. Nginx: ubuntu系统上如何判断是否安装了Nginx?

    问题描述:ubuntu系统上,如何查看是否安装了Nginx? 解决方法:输入命令行:ps -ef | grep nginx master process后面就是Nginx的安装目录. 延伸:1. 如何 ...

  4. 禅与 Objective-C 编程艺术(Zen and the Art of the Objective-C Craftsmanship)

    英文版Zen and the Art of the Objective-C Craftsmanshiphttps://github.com/objc-zen/objc-zen-book 中文版禅与 O ...

  5. iOS应用架构谈-part2 view层的组织和调用方案

    前言 <iOS应用架构谈 开篇>出来之后,很多人来催我赶紧出第二篇.这一篇文章出得相当艰难,因为公司里的破事儿特别多,我自己又有点私事儿,以至于能用来写博客的时间不够充分. 现在好啦,第二 ...

  6. kali添加更新源

    /etc/apt/sources.list 具体方法参考: http://blog.csdn.net/gmnet/article/details/14471835 http://blog.sina.c ...

  7. [图文]RHEL 7/CentOS 7/Fedora28 联网初始化

    实验说明: 入门Linux,一般会遇到以下几个问题: 从哪里获取LInux镜像? 如何通过镜像文件安装Linux系统? 安装实体机还是虚拟机? 安装完系统如何配置网络? 虚拟机的网络配置与实体机有何不 ...

  8. GoogleTest 之路3-Mocking Framework

    当你写一个原型或者测试的时候,依赖整个object 是不可行和明智的.一个 mock object和 real object 有同样的接口(所以它可以像同一个使用),但是让你在运行时进行指定它应该如何 ...

  9. stm32之PWM学习

    下图是一个STM32普通PWM形成的图形原理说明 自动重装载寄存器(ARR)用于确定波形的频率(即周期).捕获比较寄存器(CCRx)(用于确定占空比的) PWM的工作过程如下:首先ARR寄存器里面的值 ...

  10. Mysql中的联合索引、前缀索引、覆盖索引

    索引 索引是一种特殊的文件,它们包含着对数据表里所有记录的引用指针.更通俗的说,数据库索引好比是一本书前面的目录,能加快数据库的查询速度. 联合索引 又名复合索引,由两个或多个列的索引.它规定了mys ...