一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|編程技術|正則表達式|

服務器之家 - 編程語言 - JAVA教程 - 詳解在SpringBoot應用中獲取應用上下文方法

詳解在SpringBoot應用中獲取應用上下文方法

2020-09-20 14:26weiliu007 JAVA教程

本篇文章主要介紹了詳解在SpringBoot應用中獲取應用上下文方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下。

1、定義上下文工具類:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
package com.alimama.config;
 
import org.springframework.context.ApplicationContext;
/**
 * 上下文獲取工具類
 * @author mengfeiyang
 *
 */
public class SpringContextUtil {
 private static ApplicationContext applicationContext;
 
 public static void setApplicationContext(ApplicationContext context) {
  applicationContext = context;
 }
 
 public static Object getBean(String beanId) {
  return applicationContext.getBean(beanId);
 }
}

2、在啟動入口類中注入applicationContext

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.alimama;
 
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.ComponentScan;
 
import com.alimama.config.SbootConfig;
import com.alimama.config.SpringContextUtil;
import com.alimama.config.ZKConfig;
import com.alimama.quartz.InitTask;
 
/**
 * spring boot啟動入口類
 * @author mengfeiyang
 *
 */
@ComponentScan
@SpringBootApplication
@EnableConfigurationProperties({ZKConfig.class,SbootConfig.class})
public class SbootApplication implements EmbeddedServletContainerCustomizer{
 
 public static void main(String[] args) {
 ApplicationContext applicationContext = SpringApplication.run(SbootApplication.class, args);
 SpringContextUtil.setApplicationContext(applicationContext);
 }
 
 @Override
 public void customize(ConfigurableEmbeddedServletContainer container) {
 
 }
}

3、調用方法

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
package com.alimama.quartz;
 
import java.io.IOException;
 
import org.phoenix.api.action.IInterfaceAPI;
import org.phoenix.api.action.InterfaceAPI;
import org.quartz.Job;
import org.springframework.beans.factory.annotation.Autowired;
 
import com.alimama.config.SpringContextUtil;
import com.alimama.dto.TaskBean;
import com.alimama.service.IConfigService;
import com.alimama.service.impl.ConfigService;
/**
 * 任務執行者
 *
 */
public class TaskHandler implements Job{
 private ConfigService configService = (ConfigService) SpringContextUtil.getBean("configService");
 private IInterfaceAPI interf = new InterfaceAPI();
 @Override
 public void execute(JobExecutionContext arg0){
 String watchDogServer = configService.getwatchDogServer();
  System.out.println(watchDogServer);
 }
}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:https://my.oschina.net/u/1266221/blog/888257

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本激情网站 | 高清不卡一区二区 | 99re最新网址 | caoporn超碰最新地址进入 | 成人a级特黄毛片 | 视频二区 素人 欧美 日韩 | ai换脸杨颖啪啪免费网站 | 色综合色狠狠天天久久婷婷基地 | 国产精品一区二区久久不卡 | 日本理论片中文在线观看2828 | 免费观看a毛片一区二区不卡 | 男同精品视频免费观看网站 | 2022色婷婷综合久久久 | 久久这里只有精品无码3D | 四虎永久免费地址在线观看 | 大乳女子一级毛片 | 四虎永久免费地址在线网站 | 特级毛片全部免费播放器 | 国产成人免费片在线观看 | 特黄特色大片免费视频播放 | 日本国产在线视频 | 国产清纯白嫩大学生正在播放 | 色热综合| 亚洲H成年动漫在线观看不卡 | 韩国伊人| 青青青国产在线 | 好男人好资源在线观看 | 亚洲 欧美 国产 综合首页 | 久久国产加勒比精品无码 | 亚洲情射| 日韩资源在线 | 图片一区 | 2012年免费中文视频 | 精精国产www视频在线观看免费 | 不卡一区二区三区卡 | 国产婷婷高清在线观看免费 | 精品免费tv久久久久久久 | 国产黄色大片网站 | 91九色视频无限观看免费 | 娇妻中日久久持久久 | 亚洲xxxxxhd奶水女人 |