Spring Context Utils 获取上下文中的 bean

在使用多线程环境时,有些对象不是拖管在 Spring 容器中,但需要拿到 Bean 资源,是无法通过注入的方式的,就需要通过外部工具类来获取 Spring 上下文,再获取 Bean。

SpringContextUtil

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
@Component
public class SpringContextUtil implements ApplicationContextAware {

private static ApplicationContext context;

public static void setApplicationContext(ApplicationContext applicationContext) {
context = applicationContext;
}

//获取applicationContext
public static ApplicationContext getApplicationContext() {
return context;
}

//通过name获取 Bean.
/*public static Object getBean(String name) {
return context.getBean(name);
}*/

//通过name获取 Bean.
public static <T> T getBean(String beanName) {
return (T) getApplicationContext().getBean(beanName);
}

//通过class获取Bean.
public static <T> T getBean(Class<T> clazz) {
return getApplicationContext().getBean(clazz);
}

//通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name, Class<T> clazz) {
return getApplicationContext().getBean(name, clazz);
}

}

若是 SSM 框架项目,则需要在 XML 文件中将 SpringContextUtil 配置为 Bean

1
2
<!-- 注册为 bean -->
<bean id="springContextUtil" class="com.xxxxx.SpringContextUtil" scope="singleton" />

SpringUtils

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
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.ApplicationEvent;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
* Spring工具类
*/
@Slf4j
@Component
public class SpringUtil implements ApplicationContextAware {

/**
* spring全局配置项
*/
private static ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
if (applicationContext == null) {
applicationContext = context;
log.info("ApplicationContext init success,you can invoke by SpringUtil.getApplicationContext() to get ApplicationContext,init bean count="
+ applicationContext.getBeanDefinitionCount() + ",bean=" + JSONObject.toJSONString(applicationContext.getBeanDefinitionNames()));
}
}

/**
* @return spring全局配置项
*/
public static ApplicationContext getApplicationContext() {
return applicationContext;
}

/**
* 通过name获取 Bean.
*
* @param name
* @return
*/
public static <T> T getBean(String name) {
return (T) applicationContext.getBean(name);
}

/**
* 通过class获取Bean.
*
* @param clazz
* @return
*/
public static <T> T getBean(Class<T> clazz) {
return applicationContext.getBean(clazz);
}

/**
* 通过name,以及Clazz返回指定的Bean
*
* @param name
* @param clazz
* @return
*/
public static <T> T getBean(String name, Class<T> clazz) {
return applicationContext.getBean(name, clazz);
}

/**
* 获取实现相关接口的类关系
*
* @param clazz
* @return
*/
public static <T> Map<String, T> getBeansOfType(Class<T> clazz) {
return applicationContext.getBeansOfType(clazz);
}

/**
* 发布事件
*
* @param event
*/
public static void publishEvent(ApplicationEvent event) {
applicationContext.publishEvent(event);
}

/**
* 获取环境信息
*/
public static Environment getEnvironment() {
return applicationContext.getEnvironment();
}
}

@Slf4j是基于Lombok的插件。

Spring Context Utils 获取上下文中的 bean

http://blog.gxitsky.com/2018/07/24/Utils-spring-context-util/

作者

光星

发布于

2018-07-24

更新于

2022-06-17

许可协议

评论