| | |
| | | package com.vci.ubcs.starter.web.util; |
| | | |
| | | import com.vci.ubcs.starter.web.config.AppAutoConfigure; |
| | | import org.springblade.core.tool.api.R; |
| | | import org.springframework.beans.BeansException; |
| | | import org.springframework.beans.factory.annotation.Autowired; |
| | | import org.springframework.context.ApplicationContext; |
| | | import org.springframework.context.ApplicationContextAware; |
| | | import org.springframework.context.ConfigurableApplicationContext; |
| | | import org.springframework.web.bind.annotation.PostMapping; |
| | | import org.springframework.web.bind.annotation.RequestMapping; |
| | | import org.springframework.web.bind.annotation.RestController; |
| | | |
| | | /** |
| | | * spring的上下文工具, |
| | | * 注意springmvc中的controller不应该在这里获取,因为springmvc只应该被前端调用 |
| | | * 没有使用springboot的main函数里设置并获取是因为可能会让多个服务合并到一起启动 |
| | | * @author weidy |
| | | * @date 2019/10/31 9:02 AM |
| | | */ |
| | | @RestController |
| | | @RequestMapping("/application") |
| | | public class ApplicationContextProvider implements ApplicationContextAware { |
| | | |
| | | /** |
| | | * 服务的配置 |
| | | */ |
| | | @Autowired |
| | | private AppAutoConfigure appAutoConfigure; |
| | | |
| | | /** |
| | | * 应用的上下文 |
| | | */ |
| | | private static ApplicationContext applicationContext ; |
| | | |
| | | /** |
| | | * 设置应用的上下文 |
| | | * @param applicationContext 上下文 |
| | | * @throws BeansException 出现错误时需要抛出异常给spring容器 |
| | | */ |
| | | @Override |
| | | public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| | | ApplicationContextProvider.applicationContext = applicationContext; |
| | | } |
| | | |
| | | /** |
| | | * 获取应用的上下文 |
| | | * @return 上下文 |
| | | */ |
| | | public static ApplicationContext getApplicationContext() { |
| | | return applicationContext; |
| | | } |
| | | |
| | | /** |
| | | * 获取对象 |
| | | * @param name bean的名称 |
| | | * @return Object bean的对象 |
| | | * @throws BeansException 如果不存在会抛出异常 |
| | | */ |
| | | public static Object getBean(String name) throws BeansException { |
| | | return applicationContext.getBean(name); |
| | | } |
| | | |
| | | /** |
| | | * 获取对象 |
| | | * @param c bean的接口类 |
| | | * @param <T> bean所属类的类型 |
| | | * @return bean对象 |
| | | * @throws BeansException 如果不存在会抛出异常 |
| | | */ |
| | | public static <T> T getBean(Class<T> c) throws BeansException{ |
| | | return applicationContext.getBean(c); |
| | | } |
| | | |
| | | /** |
| | | * 关机 |
| | | * @return 执行完成 |
| | | */ |
| | | @PostMapping("/shutDownContext") |
| | | public R shutDownContext(String privateKey){ |
| | | if( appAutoConfigure.getPrivateTokenKey().equalsIgnoreCase(privateKey)) { |
| | | ConfigurableApplicationContext ctx = (ConfigurableApplicationContext) applicationContext; |
| | | ctx.close(); |
| | | return R.success("关闭服务成功"); |
| | | }else{ |
| | | return R.fail("您没有权限关闭服务"); |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 检查是否完成 |
| | | * @return 调用就说明成功了 |
| | | */ |
| | | @PostMapping("/checkOnline") |
| | | public R checkOnline(){ |
| | | return R.success("启动完成"); |
| | | } |
| | | |
| | | } |