激情久久久_欧美视频区_成人av免费_不卡视频一二三区_欧美精品在欧美一区二区少妇_欧美一区二区三区的

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 黄色免费播放网站 | 小视频在线看 | 九九黄色 | 欧美在线成人影院 | 欧美成人小视频 | 毛片中文字幕 | 久久里面有精品 | 久久精品一二三区 | 成人午夜网址 | 国产外围在线 | h网站在线观看 | 午夜国产在线 | 久久伊人精品视频 | 特级西西444www大精品视频免费看 | 99久久精品免费 | 99精品国产一区二区三区 | 国产日产精品一区四区介绍 | 日韩视频在线观看免费视频 | 国产精品麻豆91 | 久久久久久久久久久av | 欧美久久一区二区 | 国产在线中文 | 亚洲视频黄 | 九九视频在线观看6 | 欧美天堂一区 | 日本欧美一区二区三区视频麻豆 | 一本色道久久综合亚洲精品图片 | 精品久久久久久久久久久久久久 | 成人午夜久久 | 黄色片视频免费观看 | 精品国产高清一区二区三区 | 在线看免电影网站 | 久久精品污 | 成人午夜淫片a | 亚洲国产一区二区三区 | 手机免费看一级片 | 欧美成人一区免费视频 | 免费在线观看国产精品 | 粉嫩粉嫩一区二区三区在线播放 | 久久经典国产视频 | 91豆奶 |