Springboot上下文对象获取
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package it.benjamin.aspirinweb.mem;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
/**
* 获取Springboot上下文,通过上下文可以拿到配置文件中的配置参数
*/
@Component
public class AppContextTool implements ApplicationContextAware {
private static ApplicationContext context;
public String getConfig(String key) {
return context.getEnvironment().getProperty(key);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this .context = applicationContext;
}
}
|
或者更简单的写法:
定义一个AppUtil.java类:
1
2
3
|
public class AppUtil {
public static ConfigurableApplicationContext CONTEXT;
}
|
在启动类中进行赋值
1
2
3
|
public static void main(String[] args) {
AppUtil.CONTEXT = SpringApplication.run(RedisFlinkApplication. class , args);
}
|
spring boot获取上下文 随时取出被spring管理的bean对象
方法一:
实现ApplicationContextAware,重写setApplicationContext方法。这个方式下,工具类也被注册成了Bean,既然这样,那就必须确保该类能被Spring自动扫描到。
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 SpringContextUtils implements ApplicationContextAware {
private static ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
SpringContextUtils.applicationContext = applicationContext;
}
public static <T> T getBean(Class<T> cls) {
if (applicationContext == null ) {
throw new RuntimeException( "applicationContext注入失败" );
}
return applicationContext.getBean(cls);
}
public static Object getBean(String name) {
if (applicationContext == null ) {
throw new RuntimeException( "applicationContext注入失败" );
}
return applicationContext.getBean(name);
}
public static <T> T getBean(String name, Class<T> cls) {
if (applicationContext == null ) {
throw new RuntimeException( "applicationContext注入失败" );
}
return applicationContext.getBean(name, cls);
}
public static HttpServletRequest getRequest() {
ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder
.getRequestAttributes();
return requestAttributes == null ? null : requestAttributes.getRequest();
}
}
|
方式二:
我想要的工具类,往往会部署在公共jar中,一般情况下不能被SpringBoot程序扫描到。所有就有手动注入上下文的方式。
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
|
@Slf4j
public class SpringUtils {
private SpringUtils() {
}
private static ApplicationContext context = null ;
/**
* 初始化Spring上下文
*
* @param ctx 上下文对象
*/
public static void initContext(ApplicationContext ctx) {
if (ctx == null ) {
log.warn( "ApplicationContext is null." );
return ;
}
context = ctx;
}
/**
* 根据类型获取Bean
*
* @param cls Bean类
* @param <T> Bean类型
* @return Bean对象
*/
public static <T> T getBean(Class<T> cls) {
return context == null ? null : context.getBean(cls);
}
/**
* 根据名称获取Bean
*
* @param name Bean名称
* @return Bean对象
*/
public static Object getBean(String name) {
return context == null ? null : context.getBean(name);
}
/**
* 根据Bean名称和类获取Bean对象
*
* @param name Bean名称
* @param cls Bean类
* @param <T> Bean类型
* @return Bean对象
*/
public static <T> T getBean(String name, Class<T> cls) {
return context == null ? null : context.getBean(name, cls);
}
}
|
此种方式不用实现ApplicationContextAware接口,但是需要手动设置上下文。所以在程序入口处,需要调用initContext方法,完成上下文的初始化。
1
2
3
4
|
public static void main(String[] args) {
ApplicationContext context = SpringApplication.run(YCloudsServiceBApplication. class , args);
SpringUtils.initContext(context);
}
|
GitHub地址:https://github.com/ye17186/spring-boot-learn
以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/Vector97/article/details/114180694