Spring的Aware接口實(shí)現(xiàn)及執(zhí)行順序詳解
一、實(shí)現(xiàn)了Aware的接口
Spring中有很多繼承于aware中的接口,這些接口到底是做什么用到的,下面我們就一起來(lái)看看吧。
Aware 接口用于注入一些與容器相關(guān)信息,例如:
EnvironmentAware
:實(shí)現(xiàn)EnvironmentAware接口重寫(xiě)setEnvironment方法后,可以獲得配置文件屬性值
BeanClassLoaderAware
:注入加載當(dāng)前 bean 的 ClassLoader
BeanNameAware
: 注入當(dāng)前 bean 對(duì)應(yīng) beanName
BeanFactoryAware
: 注入 當(dāng)前BeanFactory容器 的引用
ApplicationContextAware
: 獲取容器本身ApplicationContext對(duì)象,可以在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)聽(tīng)器和觀察者設(shè)計(jì)模式的混合,一般指具備由Spring 容器通過(guò)Aware回調(diào)方法通知的特定的bean對(duì)象,簡(jiǎn)單來(lái)說(shuō)就是可以通過(guò)實(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; } /** * 獲取屬性,直接傳入屬性名稱(chēng)即可 * @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í)行順序
定義類(lèi)SimpleAware實(shí)現(xiàn)了部分Aware的接口的自定義bean,經(jīng)過(guò)程序運(yùn)行測(cè)試其執(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í)行順序的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
小白必看toString(),String.valueOf,(String)強(qiáng)轉(zhuǎn)
在Java中,往往需要把一個(gè)類(lèi)型的變量轉(zhuǎn)換成String 類(lèi)型,本文主要介紹了toString(),String.valueOf,(String)強(qiáng)轉(zhuǎn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06MySQL中關(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問(wèn)題
這篇文章主要介紹了使用JPA主鍵@Id,@IdClass,@Embeddable,@EmbeddedId問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06Visual?Studio?Code配置Tomcat運(yùn)行Java?Web項(xiàng)目詳細(xì)步驟
VS Code是一款非常棒的文本編輯器,具有配置簡(jiǎn)單、功能豐富、輕量簡(jiǎn)潔的特點(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)分頁(yè)查詢(xún)
如何使用jpa進(jìn)行多條件查詢(xún)以及查詢(xún)列表分頁(yè)呢?下面我將介紹兩種多條件查詢(xún)方式。具體實(shí)例代碼大家參考下本文吧2017-07-07Spring Cloud中使用Feign,@RequestBody無(wú)法繼承的解決方案
這篇文章主要介紹了Spring Cloud中使用Feign,@RequestBody無(wú)法繼承的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-10-10Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例
本篇文章主要介紹了Spring Boot與RabbitMQ結(jié)合實(shí)現(xiàn)延遲隊(duì)列的示例,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-11-11JDBC 實(shí)現(xiàn)通用的增刪改查基礎(chǔ)類(lèi)方法
下面小編就為大家分享一篇JDBC 實(shí)現(xiàn)通用的增刪改查基礎(chǔ)類(lèi)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-01-01JAVA多線(xiàn)程CountDownLatch使用詳解
這篇文章主要為大家詳細(xì)介紹了JAVA多線(xiàn)程CountDownLatch的使用,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-01-01