详解在SpringBoot应⽤中获取应⽤上下⽂⽅法1、定义上下⽂⼯具类:
package fig;
import t.ApplicationContext;
/**
* 上下⽂获取⼯具类
* @author mengfeiyang
*
*/
public class SpringContextUtil {
private static ApplicationContext applicationContext;
public static void setApplicationContext(ApplicationContext context) {
applicationContext = context;
}
public static Object getBean(String beanId) {
Bean(beanId);
}
}
2、在启动⼊⼝类中注⼊applicationContext
package com.alimama;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.t.embedded.ConfigurableEmbeddedServletContainer;
import org.t.embedded.EmbeddedServletContainerCustomizer;
import org.t.properties.EnableConfigurationProperties;
import t.ApplicationContext;
import t.annotation.ComponentScan;
import fig.SbootConfig;
import fig.SpringContextUtil;
import fig.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 fig.SpringContextUtil;
import com.alimama.dto.TaskBean;
import com.alimama.service.IConfigService;
import com.alimama.service.impl.ConfigService;
/**
* 任务执⾏者spring framework是什么系统
*
*/
public class TaskHandler implements Job{
private ConfigService configService = (ConfigService) Bean("configService"); private IInterfaceAPI interf = new InterfaceAPI();
@Override
public void execute(JobExecutionContext arg0){
String watchDogServer = watchDogServer();
System.out.println(watchDogServer);
}
}
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论