详解在SpringBoot应用中获取应用上下文方法

 更新时间:2017年04月27日 17:07:32   作者:weiliu007  
本篇文章主要介绍了详解在SpringBoot应用中获取应用上下文方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下。

1、定义上下文工具类:

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

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、调用方法

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);
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Java如何实现可折叠Panel方法示例

    Java如何实现可折叠Panel方法示例

    这篇文章主要给大家介绍了关于利用Java如何实现可折叠Panel的相关资料,文中通过示例代码介绍的非常详细,对大家学习或者使用java具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2018-07-07
  • 为什么Java项目中别用!=null做判空

    为什么Java项目中别用!=null做判空

    本文主要介绍了为什么Java项目中别用!=null做判空,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-04-04
  • 解决IDEA target文件夹越来越大的问题

    解决IDEA target文件夹越来越大的问题

    这篇文章主要介绍了解决IDEA target文件夹越来越大的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-02-02
  • Java多线程用法的实例详解

    Java多线程用法的实例详解

    这篇文章主要介绍了Java多线程用法的实例详解的相关资料,希望通过本文大家能够理解掌握这部分内容,需要的朋友可以参考下
    2017-09-09
  • Spring集成Web环境的实例详解

    Spring集成Web环境的实例详解

    这篇文章主要介绍了Spring集成Web环境,本文通过实例图文相结合给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-02-02
  • maven项目引用外部jar包的方法

    maven项目引用外部jar包的方法

    本篇文章主要介绍了maven项目引用外部jar的方法,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-12-12
  • Java 8实现图片BASE64编解码

    Java 8实现图片BASE64编解码

    Java 8终于有了标准的方法来处理base64的编解码。Java一直缺少BASE64编码API,以至于通常在项目开发中会选用第三方的API实现。但是,Java 8实现了BASE64编解码API,它包含到java.util包。下面这篇文章我会对Java 8的BASE64编解码做一个详细的介绍。
    2016-10-10
  • 详解时间轮TimeWheel的工作原理

    详解时间轮TimeWheel的工作原理

    时间轮(TimeWheel)作为一种高效率的计时器实现方案,在1987年发表的论文Hashed and Hierarchical Timing Wheels中被首次提出。本文主要来聊聊它的工作原理,感兴趣的可以了解一下
    2023-02-02
  • java~springboot~ibatis数组in查询的实现方法

    java~springboot~ibatis数组in查询的实现方法

    这篇文章主要介绍了java~springboot~ibatis数组in查询的实现方法,需要的朋友可以参考下
    2018-09-09
  • springboot 错误处理小结

    springboot 错误处理小结

    在 java web开发过程中,难免会有一些系统异常或人为产生一些异常。在 RESTful springboot 项目中如何优雅的处理?下面脚本之家小编给大家带来了springboot 错误处理小结,感兴趣的朋友一起看看吧
    2018-03-03

最新评论