这是我参与更文挑战的第1天,活动详情查看: 更文挑战
1.前言
日常开发过程中你可能需要根据名称从IOC
容器中获取Bean
对象、可能需要根据类型从IOC
容器中获取一组Bean
对象、也可能需要根据key
读取配置文件中的value
。
从IOC
容器中获取Bean
你需要使用ApplicationContext
,从配置文件中读取配置你需要使用Environment
,那么ApplicationContext
和Environment
你应该如何获取呢?
2.ApplicationContext
2.1 实现ApplicationContextAware接口
xxxAware
接口有一种赋能作用,就是你想拥有什么能力,只需要实现对应的接口即可。比如想拥有获取容器内容能力,只需要实现ApplicationContextAware
接口
@Component
public class RegisterEventPublisher implements ApplicationContextAware {
private ApplicationContext applicationContext;
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}
}
复制代码
2.2 监听ApplicationContextInitializedEvent事件
想拥有获取容器内容能力,除了实现ApplicationContextAware
接口之外,你还可以监听ApplicationContextInitializedEvent
事件
public class ApplicationContextInitializedEventListener implements ApplicationListener<ApplicationContextInitializedEvent> {
@Override
public void onApplicationEvent(ApplicationContextInitializedEvent event) {
ApplicationContextUtils.setApplicationContext(event.getApplicationContext());
}
}
复制代码
3.Environment
3.1 实现EnvironmentAware接口
同2.1
章节一致,想要拥有获取配置能力,只需要实现EnvironmentAware
接口
@Component
public class xxxx implements EnvironmentAware {
private Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
}
复制代码
3.2 监听ApplicationEnvironmentPreparedEvent事件
想要拥有获取配置能力,除了实现EnvironmentAware
接口之外,你还可以监听ApplicationEnvironmentPreparedEvent
事件
public class ApplicationEnvironmentListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ApplicationContextUtils.setEnvironment(event.getEnvironment());
}
}
复制代码
由于ApplicationEnvironmentPreparedEvent
事件是在容器刷新之前发布的,因此通过@Component
注解标注不会生效,需要在spring.factories
中指定
org.springframework.context.ApplicationListener= \
com.boot.example.listener.ApplicationEnvironmentListener,\
com.boot.example.listener.ApplicationContextInitializedEventListener
复制代码
4.封装
通过上面介绍你已经知道了如何获取ApplicationContext
和Environment
。实现接口的方式实际使用过程中会有些繁琐,每个想用到的地方必须满足两个条件
- 实现xxxAware接口
- 本身是一个
Bean
也就是要被@Component
注解标注
鉴于此,我们可以选择使用监听器方式封装一个工具类,这样可以达到随用随取的作用
4.1 工具类封装
public class ApplicationContextUtils {
private static ApplicationContext applicationContext;
private static Environment environment;
private ApplicationContextUtils() {
}
public static void setApplicationContext(ApplicationContext applicationContext) {
ApplicationContextUtils.applicationContext = applicationContext;
}
public static ApplicationContext getApplicationContext() {
return ApplicationContextUtils.applicationContext;
}
public static void setEnvironment(Environment environment) {
ApplicationContextUtils.environment = environment;
}
public static Environment getEnvironment() {
return ApplicationContextUtils.environment;
}
}
复制代码
4.2 监听ApplicationContextInitializedEvent事件
public class ApplicationContextInitializedEventListener implements ApplicationListener<ApplicationContextInitializedEvent> {
@Override
public void onApplicationEvent(ApplicationContextInitializedEvent event) {
ApplicationContextUtils.setApplicationContext(event.getApplicationContext());
}
}
复制代码
4.3 监听ApplicationEnvironmentPreparedEvent事件
public class ApplicationEnvironmentListener implements ApplicationListener<ApplicationEnvironmentPreparedEvent> {
@Override
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
ApplicationContextUtils.setEnvironment(event.getEnvironment());
}
}
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END