ludc
2023-05-17 b642d244b6df736ecd390368a9ffff72ddad56e5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
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("启动完成");
    }
 
}