• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号
您的位置:首页 > 程序设计 >Java > 详解在SpringBoot应用中获取应用上下文方法

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

作者:weiliu007 字体:[增加 减小] 来源:互联网 时间:2017-05-28

weiliu007 通过本文主要向大家介绍了springboot详解,springboot注解详解,联系上下文的方法,springboot是什么,springboot教程等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

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

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

</div>

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

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

</div>
分享到:QQ空间新浪微博腾讯微博微信百度贴吧QQ好友复制网址打印

您可能想查找下面的文章:

  • springboot整合freemarker详解
  • 详解SpringBoot AOP 拦截器(Aspect注解方式)
  • 详解SpringBoot配置devtools实现热部署
  • springboot全局异常处理详解
  • 详解springboot + profile(不同环境读取不同配置)
  • springboot中thymeleaf模板使用详解
  • 详解springboot+mybatis多数据源最简解决方案
  • 详解springboot整合mongodb
  • 详解在SpringBoot应用中获取应用上下文方法
  • 详解springboot配置多个redis连接

相关文章

  • 2017-05-28SpringMVC4+MyBatis+SQL Server2014实现数据库读写分离
  • 2017-05-28Java Reference源码解析
  • 2017-05-28Java微信公众平台开发(2) 微信服务器post消息体的接收
  • 2017-05-28java用重定向方法从文件中读入或写入数据
  • 2017-05-28Tomcat数据源配置方法_JBuilder中
  • 2017-05-28javaweb中mysql数据库连接步骤方法及其实例
  • 2017-05-28详解 Java 中 equals 和 == 的区别
  • 2017-05-28SpringBoot下的值注入(推荐)
  • 2017-05-28spring mvc 使用kaptcha配置生成验证码实例
  • 2017-05-28spring整合redis缓存并以注解(@Cacheable、@CachePut、@CacheEvict)形式使用

文章分类

  • JavaScript
  • ASP.NET
  • PHP
  • 正则表达式
  • AJAX
  • JSP
  • ASP
  • Flex
  • XML
  • 编程技巧
  • Android
  • swift
  • C#教程
  • vb
  • vb.net
  • C语言
  • Java
  • Delphi
  • 易语言
  • vc/mfc
  • 嵌入式开发
  • 游戏开发
  • ios
  • 编程问答
  • 汇编语言
  • 微信小程序
  • 数据结构
  • OpenGL
  • 架构设计
  • qt
  • 微信公众号

最近更新的内容

    • 老生常谈Log4j和Log4j2的区别(推荐)
    • spring boot配合前端实现跨域请求访问
    • 序列化版本号serialVersionUID的作用_动力节点Java学院整理
    • Java Scaner类详解_动力节点Java学院整理
    • java springmvc乱码解决归纳整理详解
    • 详谈Jedis连接池的使用
    • Java微信公众平台开发(8) 多媒体消息回复
    • Java IO流对象的序列化和反序列化实例详解
    • java 获取用户的MAC地址多种方法实例详解
    • Mybatis开发环境搭建实现数据的增删改查功能

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有