利用ApplicationContextAware制作一个获取ApplicationContext的Provider

1. 定义一个继承 ApplicationContextAware 的 bean

定义一个类ApplicationContextProvider,继承ApplicationContextAware,并用@Component注册一个bean

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Component
public class ApplicationContextProvider implements ApplicationContextAware {

private ApplicationContext applicationContext;

@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.applicationContext = applicationContext;
}

public ApplicationContext getContext() {
return applicationContext;
}

}

2. 使用

1
2
3
4
5
6
7
8
9
10
11
12
@Service
public class UseSample {
@Autoware
private ApplicationContextProvider applicationContextProvider;

public void sample(){
ApplicationContext appctx = applicationContextProvider.getContext();
......
......

}
}

3. 实现原理

spirng初始化的bean时,将会查看这个bean是否实现了ApplicationContextAware接口。如果是,将会调用
setApplicationContext()方法。我们在ApplicationContextProvider在实现把AppContext的地址保存到了私有变量中。

spring源码:

1
2
3
4
5
6
private void invokeAwareInterfaces(Object bean) {
.....
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware)bean).setApplicationContext(this.applicationContext);
}
}