houzhongyi
2024-07-11 e7c1260db32209a078a962aaa0ad5492c35774fb
提交 | 用户 | 时间
e7c126 1 package com.xxl.job.admin.core.util;
H 2
3 import org.springframework.context.ApplicationContext;
4 import org.springframework.context.ApplicationContextAware;
5 import org.springframework.stereotype.Component;
6
7 @Component
8 public class SpringContextUtil implements ApplicationContextAware {
9     private static ApplicationContext applicationContext;
10
11     public static ApplicationContext getApplicationContext() {
12         return applicationContext;
13     }
14
15     // 下面的这个方法上加了@Override注解,原因是继承ApplicationContextAware接口是必须实现的方法
16     @Override
17     public void setApplicationContext(ApplicationContext applicationContext) {
18         SpringContextUtil.applicationContext = applicationContext;
19     }
20
21     public static Object getBean(String name) {
22         return applicationContext.getBean(name);
23     }
24
25     public static Object getBean(String name, Class<?> requiredType) {
26
27         return applicationContext.getBean(name, requiredType);
28     }
29
30     public static <T> T getBean(Class<T> clazz) {
31         return applicationContext.getBean(clazz);
32     }
33
34     public static boolean containsBean(String name) {
35         return applicationContext.containsBean(name);
36     }
37
38     public static boolean isSingleton(String name) {
39         return applicationContext.isSingleton(name);
40     }
41
42     public static Class<?> getType(String name) {
43         return applicationContext.getType(name);
44     }
45
46     public static String[] getAliases(String name) {
47         return applicationContext.getAliases(name);
48     }
49 }