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. HZOI20190816模拟23 mine/water/gcd

    A:mine 只是一个简单的dp....是博主太蒻了... 设f[i][j],表示到第i位,状态是j的方案数,其中$j\in[0,5]$ j==0表示填0,j==1表示填1,且i-1位是雷; j==2 ...

  2. 2018CCPC吉林赛区 | 部分题解 (HDU6555 HDU6556 HDU6559 HDU6561)

    // 杭电上的重现赛:http://acm.hdu.edu.cn/contests/contest_show.php?cid=867 // 杭电6555~6566可交题 A - The Fool 题目 ...

  3. CF 1281B Azamon Web Services

    原题链接:http://codeforces.com/problemset/problem/1281/B 题目大意: 给你两个字符串 s 和 c ,最多经过一次变换,使s的字典序小于c,输出变换后的s ...

  4. 05_Spring AOP原理

    理解AOP相关概念 Target(目标对象):代理的目标对象 Joinpoint(连接点):所谓连接点是指那些被拦截到的点.在spring中,这些点指的是方法,因为spring只支持方法类型的连接点. ...

  5. 01_Hibernate持久化

    一.简介 思考:为什么使用Hibernate? Hibernate对JDBC访问数据库的代码进行了封装. Hibernate是一个基于JDBC的主流持久化框架. Hibernate的性能比较好,它是一 ...

  6. 新启vue_cli项目+引入Element

    [1]安装vue_cli vue init webpack 项目名字 [2]安装Element-UI cnpm install element-ui -S //写入dependencies cnpm ...

  7. js时间比较大小,时间加减

    第一种: //时间类比较 startTime= new Date(Date.parse(starttime)); endTime=new Date(Date.parse(endTime)); //进行 ...

  8. No context type was found in the assembly

    如果解决方法中有多个项目存在,记住要在默认项目中选择你需要的项目进行 enable-migrations    add-migration 以及updatebase

  9. DBUtils(DataSourceUtils提供数据源)

    DBUtils是apache组织的一个工具类,jdbc的框架,更方便我们使用 使用步骤: 1.导入jar包(commons-dbutils-1.4.jar,c3p0-0.9.1.2.jar) 1.1导 ...

  10. Android 开发 防止按键连续点击

    前言 按键防止连续点击是任何一个项目都要考虑的功能.下面我们将介绍几种防止按键连续点击的方法 用工具类实现 /** *@content:按键延时工具类,用于防止按键连点 *@time:2019-5-1 ...