探究實現Aware接口的原理及使用
前言
spring 對bean的創(chuàng)建過程做了很完整的封裝。但是提供了非常多的擴展接口,供我們使用。這一節(jié)主要是實現spring提供的獲取 beanFactory,classLoader 等屬性的能力。 在我們開發(fā)過程中我們經常會使用到 ApplicationContextAware
接口,來獲取到 spring的上下文。來完成對bean的獲取,當拿到了BeanFactory
以后,我們能做的東西就多起來了,我們可以通過的spring工廠獲取到我們需要的類,等等。
設計&實現
spring 提供Aware
接口機制,給外部的類提供獲取spring內部信息的能力。目前spring常用的Aware
接口有
Aware 感知接口
Aware
接口,只做標記。類似于Serializable
序列化接口,僅標記這個類可以序列化。Aware 僅表示實現類具有在獲取springbean創(chuàng)建過程中的一些內部屬性的能力。
/** * 只做標記 * 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); } // 注冊實現了 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); } } ..... }
測試
實現 需要添加特定能力的 Aware接口,實現他們的方法
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()); }
以上就是探究實現Aware接口的原理及使用的詳細內容,更多關于Aware接口原理使用的資料請關注腳本之家其它相關文章!