Spring的Aware接口實(shí)現(xiàn)及執(zhí)行順序詳解
一、實(shí)現(xiàn)了Aware的接口
Spring中有很多繼承于aware中的接口,這些接口到底是做什么用到的,下面我們就一起來看看吧。
Aware 接口用于注入一些與容器相關(guān)信息,例如:
EnvironmentAware:實(shí)現(xiàn)EnvironmentAware接口重寫setEnvironment方法后,可以獲得配置文件屬性值
BeanClassLoaderAware:注入加載當(dāng)前 bean 的 ClassLoader
BeanNameAware: 注入當(dāng)前 bean 對應(yīng) beanName
BeanFactoryAware: 注入 當(dāng)前BeanFactory容器 的引用
ApplicationContextAware: 獲取容器本身ApplicationContext對象,可以在bean中得到bean所在的應(yīng)用上下文
EmbeddedValueResolverAware:獲取properties文件單個(gè)屬性值,一般使用@Value屬性值;EmbeddedValueResolverAware是另一種基于Spring解析獲取 properties 文件單個(gè)屬性值的方式
ApplicationEventPublisherAware:事件發(fā)布器的接口,使用這個(gè)接口,我們自己的bean就擁有了發(fā)布事件的能力。
ResourceLoaderAware:獲取ResourceLoader實(shí)例,獲取資源加載器讀取資源文件
二、為什么要使用 Aware 接口
Aware接口是回調(diào),監(jiān)聽器和觀察者設(shè)計(jì)模式的混合,一般指具備由Spring 容器通過Aware回調(diào)方法通知的特定的bean對象,簡單來說就是可以通過實(shí)現(xiàn)該接口拿到Spring容器內(nèi)部的一些資源。實(shí)際的回調(diào)方法由各個(gè)子接口確定,通常應(yīng)僅包含一個(gè)接受單個(gè)參數(shù)、返回 void 的方法;但是一般情況下,Spring不建議使用該接口,因?yàn)槭褂眠@種方式會(huì)將業(yè)務(wù)代碼與Spring 耦合,有所違背IOC 原則。
1、BeanNameAware
public class MyBeanName implements BeanNameAware {
@Override
public void setBeanName(String beanName) {
System.out.println(beanName);
}
}
2、BeanFactoryAware
public class MyBeanFactory implements BeanFactoryAware {
private BeanFactory beanFactory;
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}
public void getMyBeanName() {
MyBeanName myBeanName = beanFactory.getBean(MyBeanName.class);
System.out.println(beanFactory.isSingleton("myCustomBeanName"));
}
}
3、EnvironmentAware
@Configuration
public class MyBatisConfig implements EnvironmentAware {
private static Environment environment;
@Override
public void setEnvironment(Environment environment) {
this.environment = environment;
}
@Bean
public DataSource druidDataSource() throws Exception {
Properties props = new Properties();
props.put("driverClassName", environment.getProperty("datasource.driverClassName"));
props.put("url", environment.getProperty("datasource.url"));
props.put("username", environment.getProperty("datasource.username"));
props.put("password", environment.getProperty("datasource.password"));
return DruidDataSourceFactory.createDataSource(props);
}
}
4、EmbeddedValueResolverAware
@Component
public class PropertiesUtil implements EmbeddedValueResolverAware {
private StringValueResolver resolver;
@Override
public void setEmbeddedValueResolver(StringValueResolver resolver) {
this.resolver = resolver;
}
/**
* 獲取屬性,直接傳入屬性名稱即可
* @param key
* @return
*/
public String getPropertiesValue(String key) {
StringBuilder name = new StringBuilder("${").append(key).append("}");
return resolver.resolveStringValue(name);
}
}
public class MyBean implements BeanNameAware, ApplicationContextAware, InitializingBean {
private static final Logger log = LoggerFactory.getLogger(MyBean.class);
@Override
public void setBeanName(String name) {
log.debug("當(dāng)前bean " + this + " 名字叫:" + name);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
log.debug("當(dāng)前bean " + this + " 容器是:" + applicationContext);
}
@Override
public void afterPropertiesSet() throws Exception {
log.debug("當(dāng)前bean " + this + " 初始化");
}
}
三、Aware接口執(zhí)行順序
定義類SimpleAware實(shí)現(xiàn)了部分Aware的接口的自定義bean,經(jīng)過程序運(yùn)行測試其執(zhí)行順序如下:
@Component
public class SimpleAware implements
ApplicationContextAware,
ApplicationEventPublisherAware,
BeanClassLoaderAware,
BeanFactoryAware,
BeanNameAware,
ResourceLoaderAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware 回調(diào)");
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
System.out.println("ApplicationEventPublisherAware 回調(diào)");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("BeanClassLoaderAware 回調(diào)");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware 回調(diào)");
}
@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware 回調(diào)");
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
System.out.println("ResourceLoaderAware 回調(diào)");
}
}
按照以下順序執(zhí)行:
BeanNameAware-->BeanClassLoaderAware-->BeanFactoryAware-->ResourceLoaderAware-->ApplicationEventPublisherAware-->ApplicationContextAware

以上就是Spring的Aware接口實(shí)現(xiàn)及執(zhí)行順序詳解的詳細(xì)內(nèi)容,更多關(guān)于Spring Aware接口執(zhí)行順序的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
小白必看toString(),String.valueOf,(String)強(qiáng)轉(zhuǎn)
在Java中,往往需要把一個(gè)類型的變量轉(zhuǎn)換成String 類型,本文主要介紹了toString(),String.valueOf,(String)強(qiáng)轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
MySQL中關(guān)鍵字UNION和UNION ALL的區(qū)別
本文主要介紹了MySQL中關(guān)鍵字UNION和UNION ALL的區(qū)別,深入探討UNION和UNION ALL的定義、用法、主要區(qū)別,具有一定的參考價(jià)值,感興趣的可以了解一下2024-06-06
使用JPA主鍵@Id,@IdClass,@Embeddable,@EmbeddedId問題
這篇文章主要介紹了使用JPA主鍵@Id,@IdClass,@Embeddable,@EmbeddedId問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Visual?Studio?Code配置Tomcat運(yùn)行Java?Web項(xiàng)目詳細(xì)步驟
VS Code是一款非常棒的文本編輯器,具有配置簡單、功能豐富、輕量簡潔的特點(diǎn),并且極其適合處理中小規(guī)模的代碼,這篇文章主要給大家介紹了關(guān)于Visual?Studio?Code配置Tomcat運(yùn)行Java?Web項(xiàng)目的詳細(xì)步驟,需要的朋友可以參考下2023-11-11
在Spring Boot中使用Spring-data-jpa實(shí)現(xiàn)分頁查詢
如何使用jpa進(jìn)行多條件查詢以及查詢列表分頁呢?下面我將介紹兩種多條件查詢方式。具體實(shí)例代碼大家參考下本文吧2017-07-07
Spring Cloud中使用Feign,@RequestBody無法繼承的解決方案
這篇文章主要介紹了Spring Cloud中使用Feign,@RequestBody無法繼承的解決方案,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10
Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例
本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11
JDBC 實(shí)現(xiàn)通用的增刪改查基礎(chǔ)類方法
下面小編就為大家分享一篇JDBC 實(shí)現(xiàn)通用的增刪改查基礎(chǔ)類方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01

