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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色女阁 | 动漫精品一区二区三区3d | 91精品国产综合久久 | 手机看片1024国产 | 国产精品久久香蕉免费播放 | 2022国产麻豆剧传媒剧情 | 日本午夜vr影院新入口 | freexxxx性护士第一次 | 国产成人在线播放视频 | 国产播放啪视频免费视频 | 青青草99热久久 | 国产欧美综合一区二区 | 国产情侣视频观看 | 欧美精品一区二区在线观看 | 日韩不卡一区二区三区 | 成年女人免费 | 国产永久免费爽视频在线 | 我不卡影院手机在线观看 | 3d动漫美女被吸乳羞羞视频 | 国产精品www视频免费看 | xxx黑人又大粗又长 xxxx性欧美极品另类 | 韩剧消失的眼角膜免费完整版 | 国产亚洲玖玖玖在线观看 | 无颜之月全集免费观看 | 无颜之月全集免费观看 | 精品久久久麻豆国产精品 | 日本激情网站 | 能播放的欧美同性videos | 久久精品在现线观看免费15 | 6969精品视频在线观看 | 6080午夜| 男人搡女人视频免费看 | 视频一区二区三区在线观看 | 国产欧美一区二区精品性色99 | 亚洲欧美日韩国产综合专区 | 国产情侣偷国语对白 | 国自产拍在线天天更新91 | 成人欧美1314www色视频 | 国产偷窥女洗浴在线观看亚洲 | 极品ts赵恩静和直男激战啪啪 | 女王脚奴vk |