|
| 1 | +### SpringBoot 获取应上下文 ApplicationContent ### |
| 2 | +#### 1、定义上下文工具类: #### |
| 3 | +```java |
| 4 | +package com.alimama.config; |
| 5 | + |
| 6 | +import org.springframework.context.ApplicationContext; |
| 7 | +/** |
| 8 | + * 上下文获取工具类 |
| 9 | + * @author mengfeiyang |
| 10 | + * |
| 11 | + */ |
| 12 | +public class SpringContextUtil { |
| 13 | + private static ApplicationContext applicationContext; |
| 14 | + |
| 15 | + public static void setApplicationContext(ApplicationContext context) { |
| 16 | + applicationContext = context; |
| 17 | + } |
| 18 | + |
| 19 | + public static Object getBean(String beanId) { |
| 20 | + return applicationContext.getBean(beanId); |
| 21 | + } |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +#### 2、在启动入口类中注入applicationContext #### |
| 26 | +```java |
| 27 | +package com.alimama; |
| 28 | + |
| 29 | +import org.springframework.boot.SpringApplication; |
| 30 | +import org.springframework.boot.autoconfigure.SpringBootApplication; |
| 31 | +import org.springframework.boot.context.embedded.ConfigurableEmbeddedServletContainer; |
| 32 | +import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizer; |
| 33 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 34 | +import org.springframework.context.ApplicationContext; |
| 35 | +import org.springframework.context.annotation.ComponentScan; |
| 36 | + |
| 37 | +import com.alimama.config.SbootConfig; |
| 38 | +import com.alimama.config.SpringContextUtil; |
| 39 | +import com.alimama.config.ZKConfig; |
| 40 | +import com.alimama.quartz.InitTask; |
| 41 | + |
| 42 | +/** |
| 43 | + * spring boot启动入口类 |
| 44 | + * @author mengfeiyang |
| 45 | + * |
| 46 | + */ |
| 47 | +@ComponentScan |
| 48 | +@SpringBootApplication |
| 49 | +@EnableConfigurationProperties({ZKConfig.class,SbootConfig.class}) |
| 50 | +public class SbootApplication implements EmbeddedServletContainerCustomizer{ |
| 51 | + |
| 52 | + public static void main(String[] args) { |
| 53 | + ApplicationContext applicationContext = SpringApplication.run(SbootApplication.class, args); |
| 54 | + SpringContextUtil.setApplicationContext(applicationContext); |
| 55 | + } |
| 56 | + |
| 57 | + @Override |
| 58 | + public void customize(ConfigurableEmbeddedServletContainer container) { |
| 59 | + |
| 60 | + } |
| 61 | +} |
| 62 | +``` |
| 63 | + |
| 64 | +#### 3、调用方法 #### |
| 65 | +```java |
| 66 | +package com.alimama.quartz; |
| 67 | + |
| 68 | +import java.io.IOException; |
| 69 | + |
| 70 | +import org.phoenix.api.action.IInterfaceAPI; |
| 71 | +import org.phoenix.api.action.InterfaceAPI; |
| 72 | +import org.quartz.Job; |
| 73 | +import org.springframework.beans.factory.annotation.Autowired; |
| 74 | + |
| 75 | +import com.alimama.config.SpringContextUtil; |
| 76 | +import com.alimama.dto.TaskBean; |
| 77 | +import com.alimama.service.IConfigService; |
| 78 | +import com.alimama.service.impl.ConfigService; |
| 79 | +/** |
| 80 | + * 任务执行者 |
| 81 | + * @author mengfeiyang |
| 82 | + * |
| 83 | + */ |
| 84 | +public class TaskHandler implements Job{ |
| 85 | + private ConfigService configService = (ConfigService) SpringContextUtil.getBean("configService"); |
| 86 | + private IInterfaceAPI interf = new InterfaceAPI(); |
| 87 | + @Override |
| 88 | + public void execute(JobExecutionContext arg0){ |
| 89 | + String watchDogServer = configService.getwatchDogServer(); |
| 90 | + System.out.println(watchDogServer); |
| 91 | + } |
| 92 | +} |
| 93 | +``` |
0 commit comments