Spring高級接口Aware淺析
Aware 接口功能闡述
Aware 接口提供了一種【內置】 的注入手段,例如
a.BeanNameAware 注入 bean 的名字
b.BeanFactoryAware 注入 BeanFactory 容器
c.ApplicationContextAware 注入 ApplicationContext 容器
d.EmbeddedValueResolverAware 注入 ${} 解析器
功能案例演示
public class MyBean implements BeanNameAware, ApplicationContextAware, InitializingBean {
private static final Logger log = LoggerFactory.getLogger(MyBean.class);
@Override
public void setBeanName(String name) {
log.debug("當前bean " + this + " 名字叫:" + name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
log.debug("當前bean " + this + " 容器是:" + applicationContext);
}
@Override
public void afterPropertiesSet() throws Exception {
log.debug("當前bean " + this + " 初始化");
}
@Autowired
public void aaa(ApplicationContext applicationContext) {
log.debug("當前bean " + this + " 使用@Autowired 容器是:" + applicationContext);
}
@PostConstruct
public void init() {
log.debug("當前bean " + this + " 使用@PostConstruct 初始化");
}
}測試:
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean("myBean", MyBean.class);
context.refresh();
context.close();可能這里有人會有疑問: b、c、d 的功能用 @Autowired 就能實現(xiàn)啊, 為啥還要用 Aware 接口呢
簡單地說:
a.@Autowired 的解析需要用到 bean 后處理器, 屬于擴展功能
b.而 Aware 接口屬于內置功能, 不加任何擴展, Spring 就能識別
某些情況下, 擴展功能會失效, 而內置功能不會失效
如下所示:
@Autowired
public void aaa(ApplicationContext applicationContext) {
log.debug("當前bean " + this + " 使用@Autowired 容器是:" + applicationContext);
}
@PostConstruct
public void init() {
log.debug("當前bean " + this + " 使用@PostConstruct 初始化");
}在這里,你會發(fā)現(xiàn)用 Aware 注入 ApplicationContext 成功, 而 @Autowired 注入 ApplicationContext 失敗
擴展功能失效場景分析
@Configuration
public class MyConfig1 {
private static final Logger log = LoggerFactory.getLogger(MyConfig1.class);
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) {
log.debug("注入 ApplicationContext");
}
@PostConstruct
public void init() {
log.debug("初始化");
}
@Bean // beanFactory 后處理器
public BeanFactoryPostProcessor processor1() {
return beanFactory -> {
log.debug("執(zhí)行 processor1");
};
}
}context.refresh();
這行代碼的執(zhí)行順序,到容器里找到所有的.beanFactory 后處理器來執(zhí)行,添加 bean 后處理器,初始化單例。
Java 配置類不包含 BeanFactoryPostProcessor 的情況

Java 配置類包含 BeanFactoryPostProcessor 的情況,因此要創(chuàng)建其中的 BeanFactoryPostProcessor 必須提前創(chuàng)建 Java 配置類,而此時的 BeanPostProcessor 還未準備好,導致 @Autowired 等注解失效

對應代碼
@Configuration
public class MyConfig1 {
private static final Logger log = LoggerFactory.getLogger(MyConfig1.class);
@Autowired
public void setApplicationContext(ApplicationContext applicationContext) {
log.debug("注入 ApplicationContext");
}
@PostConstruct
public void init() {
log.debug("初始化");
}
@Bean // ?? 注釋或添加 beanFactory 后處理器對應上方兩種情況
public BeanFactoryPostProcessor processor1() {
return beanFactory -> {
log.debug("執(zhí)行 processor1");
};
}
}注意
解決方法:
- 用內置依賴注入和初始化取代擴展依賴注入和初始化
- 用靜態(tài)工廠方法代替實例工廠方法,避免工廠對象提前被創(chuàng)建
測試:
GenericApplicationContext context = new GenericApplicationContext();
context.registerBean("myConfig2", MyConfig2.class);
context.registerBean(AutowiredAnnotationBeanPostProcessor.class);
context.registerBean(CommonAnnotationBeanPostProcessor.class);
context.registerBean(ConfigurationClassPostProcessor.class);
context.refresh();
context.close();輸出結果:

總結
- Aware 接口提供了一種【內置】 的注入手段, 可以注入 BeanFactory, ApplicationContext
- InitializingBean 接口提供了一種【內置】的初始化手段
- 內置的注入和初始化不受擴展功能的影響, 總會被執(zhí)行, 因此 Spring 框架內部的類常用它們
到此這篇關于Spring高級接口Aware淺析的文章就介紹到這了,更多相關Spring Aware內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
基于SpringBoot?使用?Flink?收發(fā)Kafka消息的示例詳解
這篇文章主要介紹了基于SpringBoot?使用?Flink?收發(fā)Kafka消息,本文通過示例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-01-01

