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

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

Aware 感知接口
Aware接口,只做標記。類似于Serializable序列化接口,僅標記這個類可以序列化。Aware 僅表示實現(xiàn)類具有在獲取springbean創(chuàng)建過程中的一些內(nèi)部屬性的能力。
/**
* 只做標記
* 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的擴展屬性時,給這個bean加上特定的能力
@Override
protected Object createBean(String beanName, BeanDefinition beanDefinition, Object[] args) {
Object bean = null;
try {
bean = createBeanInstance(beanDefinition, beanName, args);
// 注入屬性
applyPropertyValues(beanName, bean, beanDefinition);
// 提供給外部的擴展包裝,執(zhí)行 Bean 的初始化方法和 BeanPostProcessor 的前置和后置處理方法
bean = initializeBean(beanName, bean, beanDefinition);
} catch (Exception e) {
throw new RuntimeException("bean create error!", e);
}
// 注冊實現(xiàn)了 DisposableBean 接口的 Bean 對象
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);
}
}
.....
}測試
實現(xiàn) 需要添加特定能力的 Aware接口,實現(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());
}

以上就是探究實現(xiàn)Aware接口的原理及使用的詳細內(nèi)容,更多關(guān)于Aware接口原理使用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
關(guān)于SpringBoot啟動速度慢的原因總結(jié)
這篇文章主要介紹了關(guān)于SpringBoot啟動速度慢的原因總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-05-05

