Spring boot随时获取ApplicationContex
- @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;
- }
- }
@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;
}
}
- 或者
- @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;
- }
- }
或者
@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;
} }
- 或者直接
- @Autowired
- ApplicationContext context;
或者直接
@Autowired
ApplicationContext context;
Spring boot随时获取ApplicationContex的更多相关文章
- Spring boot Freemarker 获取ContextPath的方法
Spring boot Freemarker 获取ContextPath的两种方法: 1.自定义viewResolver,Spring boot中有一个viewResolver,这个和配置文件中的师徒 ...
- 【spring】【spring boot】获取系统根路径,根目录,用于存储临时生成的文件在服务器上
今日份代码: private static final String UPLOAD_TEMP_FILE_NAME = "测试商品数据.xlsx"; /** * 获取临时文件路径 * ...
- spring boot项目获取application配置文件参数的两种方式
前言:了解过spring boot这个技术的,应该知道spring boot的核心配置文件application.properties,当然也可以通过注解自定义配置文件**.properties的信息 ...
- 【spring】【spring mvc】【spring boot】获取spring cloud项目中所有spring mvc的请求资源
实现的方法: 1.在父级项目中 或者 每个微服务都引用的项目中添加实体类Resource 2.在父级项目中 或者 每个为服务都引用的项目中写一个工具类,作用是用来获取请求资源 3.在每一个微服务的启动 ...
- Spring Boot程序获取tomcat启动端口
package com.geostar.geostack.git_branch_manager.config; import org.springframework.beans.factory.ann ...
- 学习 Spring Boot:(二十九)Spring Boot Junit 单元测试
前言 JUnit 是一个回归测试框架,被开发者用于实施对应用程序的单元测试,加快程序编制速度,同时提高编码的质量. JUnit 测试框架具有以下重要特性: 测试工具 测试套件 测试运行器 测试分类 了 ...
- spring boot学习笔记2
开场知识: spring 容器注入bean,时容器初始化的一些接口以及接口调用的时间先后顺序: 1)BeanFactoryPostProcessor 容器初始化的回调方法 * BeanFactoryP ...
- spring boot 2.0 源码分析(二)
在上一章学习了spring boot 2.0启动的大概流程以后,今天我们来深挖一下SpringApplication实例变量的run函数. 先把这段run函数的代码贴出来: /** * Run the ...
- Spring Boot+BootStrap fileInput 多图片上传
一.依赖文件 <link rel="stylesheet" type="text/css" th:href="@{/js/bootstrap/c ...
随机推荐
- HAVING方法也是连贯操作之一
HAVING方法也是连贯操作之一,用于配合group方法完成从分组的结果中筛选(通常是聚合条件)数据. having方法只有一个参数,并且只能使用字符串,例如: $this->field('us ...
- Java英语面试题(核心知识篇)
Java英语面试题(核心知识篇) Question: What is transient variable?Answer: Transient variable can't be serialize. ...
- PHP实现图片的汉明码提取与降维
作者感言:数学不好,遇到算法问题分分钟狗带,毫无转寰的余地-_-||| 最近心血来潮,看了相似图片的搜索,最最最初级的方法即提取汉明码,之后匹配汉明距离.当然,在数以亿计的汉明码中,要筛出需要的图片, ...
- PAT甲级——A1104 Sum of Number Segments
Given a sequence of positive numbers, a segment is defined to be a consecutive subsequence. For exam ...
- python 读取excel Xlrd模块
1. 安装xlrd模块 我使用pip安装: cmd ->切换到pip安装所在路径->pip install xlrd->回车 2. 使用 2.1:打开Excel表 导入模块: im ...
- 06-6-es6模板字符串
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- css设置字体小于12px
p { white-space: nowrap; font-size: .12rem; -webkit-transform-origin-x: 0; //缩小后文字居左 -webkit-transfo ...
- nulls_hlist原理 和 tcp连接查找
原文链接 http://abcdxyzk.github.io/blog/2018/09/28/kernel-sk_lookup/
- AlexNet结构图详解
- Hbase实验:java创建和删除table
开启zookeeper.hadoop.hbase: 打开eclipse创一个java project,然后导入所需jar包: 写好java代码,运行create,然后去hbase shell里查看: ...