Spring的Aware接口實現(xiàn)及執(zhí)行順序詳解
一、實現(xiàn)了Aware的接口
Spring中有很多繼承于aware中的接口,這些接口到底是做什么用到的,下面我們就一起來看看吧。
Aware 接口用于注入一些與容器相關信息,例如:
EnvironmentAware:實現(xiàn)EnvironmentAware接口重寫setEnvironment方法后,可以獲得配置文件屬性值
BeanClassLoaderAware:注入加載當前 bean 的 ClassLoader
BeanNameAware: 注入當前 bean 對應 beanName
BeanFactoryAware: 注入 當前BeanFactory容器 的引用
ApplicationContextAware: 獲取容器本身ApplicationContext對象,可以在bean中得到bean所在的應用上下文
EmbeddedValueResolverAware:獲取properties文件單個屬性值,一般使用@Value屬性值;EmbeddedValueResolverAware是另一種基于Spring解析獲取 properties 文件單個屬性值的方式
ApplicationEventPublisherAware:事件發(fā)布器的接口,使用這個接口,我們自己的bean就擁有了發(fā)布事件的能力。
ResourceLoaderAware:獲取ResourceLoader實例,獲取資源加載器讀取資源文件
二、為什么要使用 Aware 接口
Aware接口是回調,監(jiān)聽器和觀察者設計模式的混合,一般指具備由Spring 容器通過Aware回調方法通知的特定的bean對象,簡單來說就是可以通過實現(xiàn)該接口拿到Spring容器內部的一些資源。實際的回調方法由各個子接口確定,通常應僅包含一個接受單個參數(shù)、返回 void 的方法;但是一般情況下,Spring不建議使用該接口,因為使用這種方式會將業(yè)務代碼與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("當前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 + " 初始化");
}
}
三、Aware接口執(zhí)行順序
定義類SimpleAware實現(xiàn)了部分Aware的接口的自定義bean,經(jīng)過程序運行測試其執(zhí)行順序如下:
@Component
public class SimpleAware implements
ApplicationContextAware,
ApplicationEventPublisherAware,
BeanClassLoaderAware,
BeanFactoryAware,
BeanNameAware,
ResourceLoaderAware {
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
System.out.println("ApplicationContextAware 回調");
}
@Override
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) {
System.out.println("ApplicationEventPublisherAware 回調");
}
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
System.out.println("BeanClassLoaderAware 回調");
}
@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
System.out.println("BeanFactoryAware 回調");
}
@Override
public void setBeanName(String name) {
System.out.println("BeanNameAware 回調");
}
@Override
public void setResourceLoader(ResourceLoader resourceLoader) {
System.out.println("ResourceLoaderAware 回調");
}
}
按照以下順序執(zhí)行:
BeanNameAware-->BeanClassLoaderAware-->BeanFactoryAware-->ResourceLoaderAware-->ApplicationEventPublisherAware-->ApplicationContextAware

以上就是Spring的Aware接口實現(xiàn)及執(zhí)行順序詳解的詳細內容,更多關于Spring Aware接口執(zhí)行順序的資料請關注腳本之家其它相關文章!
相關文章
SpringBoot項目中配置application.yml中server.port不生效的問題
這篇文章主要介紹了SpringBoot項目中配置application.yml中server.port不生效的問題及解決方案,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring-cloud Config Server的3種配置方式
這篇文章主要介紹了Spring-cloud Config Server的3種配置方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
IDEA連接MySQL數(shù)據(jù)庫的4種方法圖文教程
IDEA是一種流行的Java開發(fā)工具,可以方便地連接MySQL,這篇文章主要給大家介紹了關于IDEA連接MySQL數(shù)據(jù)庫的4種方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下2023-12-12
Mybatis中自定義實例化SqlSessionFactoryBean問題
這篇文章主要介紹了Mybatis中自定義實例化SqlSessionFactoryBean問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02
Java中I/O流讀取數(shù)據(jù)不完整的問題解決
本文主要介紹了ava中I/O流讀取數(shù)據(jù)不完整的問題,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2022-05-05

