1. @Service
  2. public class SpringManager implements ApplicationListener<ContextRefreshedEvent> {
  3. private static ApplicationContext applicationContext = null;
  4. @Override
  5. public void onApplicationEvent(ContextRefreshedEvent event) {
  6. if(applicationContext == null){
  7. applicationContext = event.getApplicationContext();
  8. }
  9. }
  10. /*ApplicationContext context= ContextLoader.getCurrentWebApplicationContext();//尝试下这个方法*/
  11. public static ApplicationContext getApplicationContext() {
  12. return applicationContext;
  13. }
  14. }
@Service
public class SpringManager implements ApplicationListener<ContextRefreshedEvent> {
private static ApplicationContext applicationContext = null;
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
if(applicationContext == null){
applicationContext = event.getApplicationContext();
}
}
/*ApplicationContext context= ContextLoader.getCurrentWebApplicationContext();//尝试下这个方法*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}
}

  1. 或者
  2. @Service
  3. public class SpringTool implements ApplicationContextAware {
  4. private static ApplicationContext context = null;
  5. @Override
  6. public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
  7. context = applicationContext;
  8. }
  9. public static ApplicationContext getApplicationContext() {
  10. return context;
  11. }
  12. }
或者
@Service
public class SpringTool implements ApplicationContextAware {
private static ApplicationContext context = null;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
context = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return context;
} }

  1. 或者直接  
  2.     @Autowired  
  3.     ApplicationContext context;  
或者直接

@Autowired

ApplicationContext context;

Spring boot随时获取ApplicationContex的更多相关文章

  1. Spring boot Freemarker 获取ContextPath的方法

    Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...

  2. 【spring】【spring boot】获取系统根路径,根目录,用于存储临时生成的文件在服务器上

    今日份代码: private static final String UPLOAD_TEMP_FILE_NAME = "测试商品数据.xlsx"; /** * 获取临时文件路径 * ...

  3. spring boot项目获取application配置文件参数的两种方式

    前言:了解过spring boot这个技术的,应该知道spring boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...

  4. 【spring】【spring mvc】【spring boot】获取spring cloud项目中所有spring mvc的请求资源

    实现的方法: 1.在父级项目中 或者 每个微服务都引用的项目中添加实体类Resource 2.在父级项目中 或者 每个为服务都引用的项目中写一个工具类,作用是用来获取请求资源 3.在每一个微服务的启动 ...

  5. Spring Boot程序获取tomcat启动端口

    package com.geostar.geostack.git_branch_manager.config; import org.springframework.beans.factory.ann ...

  6. 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试

    前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量. JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 了 ...

  7. spring boot学习笔记2

    开场知识: spring 容器注入bean,时容器初始化的一些接口以及接口调用的时间先后顺序: 1)BeanFactoryPostProcessor 容器初始化的回调方法 * BeanFactoryP ...

  8. spring boot 2.0 源码分析(二)

    在上一章学习了spring boot 2.0启动的大概流程以后,今天我们来深挖一下SpringApplication实例变量的run函数. 先把这段run函数的代码贴出来: /** * Run the ...

  9. Spring Boot+BootStrap fileInput 多图片上传

    一.依赖文件 <link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/c ...

随机推荐

  1. Apache Flink 1.9.0版本新功能介绍

    摘要:Apache Flink是一个面向分布式数据流处理和批量数据处理的开源计算平台,它能够基于同一个Flink运行时,提供支持流处理和批处理两种类型应用的功能.目前,Apache Flink 1.9 ...

  2. vue cnpm run dev 报错,解决方法

    执行到   $ cnpm run dev  报如下错,但是实际上 我执行   npm -v 是5.0.4 其原因是nodejs里的版本不对,解决方法

  3. Python学习day10-文件处理

    figure:last-child { margin-bottom: 0.5rem; } #write ol, #write ul { position: relative; } img { max- ...

  4. ssh连接Ubuntu之access denied

    解决方法是: 修改/etc/ssh/ssh_config文件, #PermitRootLogin prohibit-password改为PermitRootLogin yes 然后 PasswordA ...

  5. “玲珑杯”ACM比赛 Round #11 B题

    http://www.ifrog.cc/acm/problem/1097?contest=1013&no=1 //LIS的高端写法 #include <iostream> #inc ...

  6. 初识splay

    这东西都没什么板子着实让我很难受啊,只能到网上抄抄补补, 记下两个用到的博客 https://blog.csdn.net/clove_unique/article/details/50630280 h ...

  7. Luogu P1401 城市(二分+网络流)

    P1401 城市 题意 题目描述 N(2<=n<=200)个城市,M(1<=m<=40000)条无向边,你要找T(1<=T<=200)条从城市1到城市N的路,使得最 ...

  8. [转]C#中用NamedPipe进程间通信

    转自:http://blog.csdn.net/jinjazz/archive/2009/02/03/3861143.aspx 本文只是一个测试例子,核心代码是kernel32.dll中的一组wind ...

  9. cannot be cast to javax.servlet.Servlet 解决

    使用maven创建web项目的时候,通过添加依赖的方式来添加servlet-api,如下 通过maven的命令(tomcat:run)来启动项目,发现访问的时候报错,错误如下: 错误排查: 首先查看s ...

  10. ArcGIS Server 10.1安装、配置、发布地图服务

    先跟大家分享一个esri的学习资料,http://pan.baidu.com/s/1nBzxB,<ArcGIS10.1 for Server 入门教程>.教程讲述的很清楚,下面说说我这次发 ...