实际开发中我们经常需要通过spring上下文获取一些配置信息,本文阐述springboot应用获取spring上下文的几种方式。

方式一:实现ApplicationContextAware接口

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/*** 实现ApplicationContextAware接口设置applicationContext* 提供static方法供调用者使用,不要求使用者受spring容器管理*/
@Component
public class SpringContextUtil1 implements ApplicationContextAware {public static ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {SpringContextUtil1.applicationContext = applicationContext;}public static ApplicationContext getApplicationContext() {return SpringContextUtil1.applicationContext;}public static Object getBean(String name) {return getApplicationContext().getBean(name);}public static <T> T getBean(Class<T> clazz) {return getApplicationContext().getBean(clazz);}public static <T> T getBean(String name, Class<T> clazz) {return getApplicationContext().getBean(name, clazz);}
}

方式二:非static方法版

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;/*** 实现ApplicationContextAware接口设置applicationContext* 如果使用者也是被spring管理的bean则可以使用注入的方式使用,而非调用static方法*/
@Component
public class SpringContextUtil2 implements ApplicationContextAware {public ApplicationContext applicationContext;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {this.applicationContext = applicationContext;}public ApplicationContext getApplicationContext() {return this.applicationContext;}public Object getBean(String name) {return applicationContext.getBean(name);}public <T> T getBean(Class<T> clazz) {return applicationContext.getBean(clazz);}public <T> T getBean(String name, Class<T> clazz) {return getApplicationContext().getBean(name, clazz);}
}@Component
public class ContextUser{/*** 使用者直接注入*/@Autowiredpublic SpringContextUtil2 springContextUtil2;}

方式三:在springboot引导类里设置

import org.springframework.context.ApplicationContext;/*** 在springboot引导类里设置applicationContext* 工具类无需实现ApplicationContextAware接口*/public class SpringContextUtil3 {public static ApplicationContext applicationContext;public static ApplicationContext getApplicationContext() {return SpringContextUtil3.applicationContext;}public static void setApplicationContext(ApplicationContext applicationContext) {SpringContextUtil3.applicationContext = applicationContext;}public static Object getBean(String name) {return getApplicationContext().getBean(name);}public static Object getBean(Class<?> requiredType) {return getApplicationContext().getBean(requiredType);}public static <T> T getBean(String name, Class<T> clazz) {return getApplicationContext().getBean(name, clazz);}
}//在springboot引导类里设置applicationContext
@SpringBootApplication
public class MainApplication {public static void main(String[] args) {ConfigurableApplicationContext context = SpringApplication.run(MainApplication.class, args);SpringContextUtil3.setApplicationContext(context);}
}

方式四:直接注入ApplicationContext

@Component
public class ContextUser{/*** 在需要使用上下文的地方直接注入(前提:使用者受spring容器管理)*/@Autowiredpublic ApplicationContext applicationContext;}

springboot实战 获取spring上下文的4种方式相关推荐

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

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

  2. Spring获取request对象的几种方式

    参考文章: 在SpringMVC中获取request对象的几种方式 Springboot获取request和response 使用Springboot,我们很多时候直接使用@PathVariable. ...

  3. java类加入到spring容器_普通java类加入spring容器的四种方式

    今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...

  4. IoC、Spring 环境搭建、Spring 创建对象的三种方式、DI

    二.IoC 中文名称:控制反转 英文名称:(Inversion of Control) 3.I oC 是什么? 3.1 IoC 完成的事情原先由程序员主动通过 new 实例化对象事情,转交给 Spri ...

  5. python执行系统命令后获取返回值的几种方式集合

    第一种情况 os.system('ps aux') 执行系统命令,没有返回值 第二种情况 result = os.popen('ps aux') res = result.read() for lin ...

  6. 获取Class对象的三种方式

    获取Class对象的三种方式 Object --> getClass() 通过对象.getclass 任何数据类型(包括基本数据类型)都有一个"静态"的class属性 通过类 ...

  7. html中获取modelandview中的json数据_从Bitmap中获取YUV数据的两种方式

    从Bitmap中我们能获取到的是RGB颜色分量,当需要获取YUV数据的时候,则需要先提取R,G,B分量的值,然后将RGB转化为YUV(根据具体的YUV的排列格式做相应的Y,U,V分量的排列) 所以这篇 ...

  8. php解析url并得到url中的参数及获取url参数的四种方式

    本文给大家介绍php解析url并得到url中的参数及获取url参数的四种方式,涉及到将字符串参数变为数组,将参数变为字符串的相关知识,本文代码简单易懂,感兴趣的朋友一起看看吧 下面一段代码是php解析 ...

  9. 获取class文件对象三种方式

    Class类 阅读API的Class类得知,Class 没有公共构造方法.Class 对象是在加载类时由 Java 虚拟机以及通过调用类加载器中的 defineClass 方法自动构造的 获取Clas ...

最新文章

  1. 生产中NFS案例记录---写入权限解决过程
  2. 收集几个js实现的日期时间控件
  3. php mysql 表字段_php mysql获取表字段名称和字段信息的三种方法
  4. 前端学习(3102):vue+element今日头条管理-hello-react案例
  5. idea gblfy常用快捷键
  6. Windows误关闭资源管理器重启的办法
  7. Java常见排序算法之Shell排序
  8. python项目代码量_python统计项目代码行数
  9. 【1】推荐系统评测指标
  10. java for 面试题_Java面试题整理
  11. 计算机网络性能(1)
  12. 关于序列化的 10 几个问题,你肯定不知道
  13. HTML5与Flash相比有哪些好处?
  14. Excel模板下载(带下拉框)
  15. 第一次结对编程作业——需求分析与原型设计
  16. 教你如何拔取百度地图POI兴趣点
  17. 写好的java项目如何部署在公司服务器上,并能让局域网内其他同事访问到,以及遇到的坑!
  18. repair mysql_REPAIR TABLE语法--MySql数据库
  19. android 调用系统相机拍照并返回路径,Android调用相机拍照并返回路径和…
  20. Emby Server入库影片信息推送功能实现

热门文章

  1. 中国治理蝗灾是生物防治的成就,根本不是靠鸡靠鸭靠吃货换来的!
  2. 专家点评Nature Plants | 中科院微生物所郭惠珊研究组揭示土传病原菌逃避寄主免疫的新机制...
  3. 根际微生物组提高植物耐盐性的研究进展(Biotechnology Advances IF=10)
  4. 扩增子统计绘图1箱线图:Alpha多样性
  5. R语言使用treemap包中的treemap函数可视化treemap图:treemap将分层数据显示为一组嵌套矩形、自定义设置treemap图的调色板、自定义设置treemap标题字体的大小
  6. R语言ggplot2可视化使用facet_grid构建多个子图(facet、面图)并自定义每个子图(facet、面图)的文本实战
  7. R语言stringr包str_ends函数、str_starts函数起始、结束字符串判断实战
  8. 机器学习数据预处理之离群值/异常值:箱图法(boxplot method)
  9. mlxtend对sklearn进行扩展
  10. Relation Networks for Object Detection