探究實(shí)現(xiàn)Aware接口的原理及使用
前言
spring 對(duì)bean的創(chuàng)建過程做了很完整的封裝。但是提供了非常多的擴(kuò)展接口,供我們使用。這一節(jié)主要是實(shí)現(xiàn)spring提供的獲取 beanFactory,classLoader 等屬性的能力。 在我們開發(fā)過程中我們經(jīng)常會(huì)使用到 ApplicationContextAware接口,來獲取到 spring的上下文。來完成對(duì)bean的獲取,當(dāng)拿到了BeanFactory以后,我們能做的東西就多起來了,我們可以通過的spring工廠獲取到我們需要的類,等等。

設(shè)計(jì)&實(shí)現(xiàn)
spring 提供Aware接口機(jī)制,給外部的類提供獲取spring內(nèi)部信息的能力。目前spring常用的Aware接口有

Aware 感知接口
Aware接口,只做標(biāo)記。類似于Serializable序列化接口,僅標(biāo)記這個(gè)類可以序列化。Aware 僅表示實(shí)現(xiàn)類具有在獲取springbean創(chuàng)建過程中的一些內(nèi)部屬性的能力。
/**
* 只做標(biāo)記
* spring容器感知接口
*/
public interface Aware {
}
提供具體能力的接口
ApplicationContextAware 提供獲取 applicationContext 的能力
public interface ApplicationContextAware extends Aware {
void setApplicationContext(ApplicationContext applicationContext);
}
BeanClassLoaderAware提供獲取 classLoader 的能力
public interface BeanClassLoaderAware extends Aware{
void setBeanClassLoader(ClassLoader classLoader);
}
BeanFactoryAware 提供獲取 BeanFactory 的能力
public interface BeanFactoryAware extends Aware{
void setBeanFactory(BeanFactory beanFactory) throws BeansException;
}
BeanNameAware 提供獲取 beanName 的能力
public interface BeanNameAware extends Aware{
void setBeanName(String beanName);
}
他們都在創(chuàng)建bean完成后,在添加bean的擴(kuò)展屬性時(shí),給這個(gè)bean加上特定的能力
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
// 注入屬性
applyPropertyValues(beanName, bean, beanDefinition);
// 提供給外部的擴(kuò)展包裝,執(zhí)行 Bean 的初始化方法和 BeanPostProcessor 的前置和后置處理方法
bean = initializeBean(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new RuntimeException("bean create error!", e);
}
// 注冊(cè)實(shí)現(xiàn)了 DisposableBean 接口的 Bean 對(duì)象
registerDisposableBeanIfNecessary(beanName, bean, beanDefinition);
registerSingleton(beanName, bean);
return bean;
}
private Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition) throws BeansException {
if (bean instanceof Aware) {
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
if (bean instanceof ApplicationContextAware) {
((ApplicationContextAware) bean).setApplicationContext(this);
}
if (bean instanceof BeanClassLoaderAware) {
((BeanClassLoaderAware) bean).setBeanClassLoader(getClassLoader());
}
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
}
.....
}測(cè)試
實(shí)現(xiàn) 需要添加特定能力的 Aware接口,實(shí)現(xiàn)他們的方法
public class UserService implements InitializingBean, DisposableBean, ApplicationContextAware, BeanClassLoaderAware, BeanNameAware {
private ApplicationContext applicationContext;
private ClassLoader classLoader;
@Override
public void setBeanClassLoader(ClassLoader classLoader) {
this.classLoader = classLoader;
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) {
this.applicationContext = applicationContext;
}
}
@Test
public void testContext1() throws BeansException {
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("classpath:spring.xml");
applicationContext.registerShutdownHook();
UserService userService = (UserService) applicationContext.getBean("userService");
System.out.println(userService.say());
System.out.println(userService.getApplicationContext());
System.out.println(userService.getClassLoader());
System.out.println(userService.getBeanName());
}

以上就是探究實(shí)現(xiàn)Aware接口的原理及使用的詳細(xì)內(nèi)容,更多關(guān)于Aware接口原理使用的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Java前端Layer.open.btn驗(yàn)證無效解決方法
在本篇文章里我們給大家整理了一篇關(guān)于Java前端Layer.open.btn驗(yàn)證無效解決方法以及實(shí)例代碼,需要的朋友們可以參考學(xué)習(xí)下。2019-09-09
Spring boot validation校驗(yàn)方法實(shí)例
這篇文章主要給大家介紹了關(guān)于Spring boot validation校驗(yàn)方法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02
Java selenium處理極驗(yàn)滑動(dòng)驗(yàn)證碼示例
本篇文章主要介紹了Java selenium處理極驗(yàn)滑動(dòng)驗(yàn)證碼示例,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10
淺析java程序中hibernate的應(yīng)用總結(jié)
hibernate可以理解為是一個(gè)中間件它負(fù)責(zé)把java程序的sql語句接收過來發(fā)送到數(shù)據(jù)庫,而數(shù)據(jù)庫返回來的信息hibernate接收之后直接生成一個(gè)對(duì)象傳給java2013-07-07
關(guān)于SpringBoot啟動(dòng)速度慢的原因總結(jié)
這篇文章主要介紹了關(guān)于SpringBoot啟動(dòng)速度慢的原因總結(jié),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05

