从网站开发到游戏编程,有哪些免费ppt模板下载网址,宁波模板网站建站,建站报价表参考小傅哥的教程#xff1a;第09章#xff1a;Aware感知容器对象 | 小傅哥 bugstack 虫洞栈
本期的目标是实现Aware功能#xff0c;Aware是一个顶层接口#xff0c;其拥有众多子类#xff0c;如BeanClassLoaderAware、BeanNameAware、BeanFactoryAware、ApplicationCont…参考小傅哥的教程第09章Aware感知容器对象 | 小傅哥 bugstack 虫洞栈本期的目标是实现Aware功能Aware是一个顶层接口其拥有众多子类如BeanClassLoaderAware、BeanNameAware、BeanFactoryAware、ApplicationContextAware接口实现不同的Aware接口可以使Bean获得不一样的能力。举个例子假设我们的Bean实现了BeanFactoryAware接口那意味着我们的Bean拥有获取Spring中的BeanFactory类对象的权力。因此Aware接口是赋予Bean感知容器中的相关对象具体感知什么对象取决于实现了哪个Aware子类接口。实现Aware首先是顶层接口Aware其只是一个标记性接口/** * 标记类接口实现该接口可以被Spring容器感知 */ public interface Aware { }接下来是各个子接口这些子接口有一个共同特点都提供了相应的set方法。BeanFactoryAware/** * 实现此接口即能感知到所属的 BeanFactory */ public interface BeanFactoryAware extends Aware { void setBeanFactory(BeanFactory beanFactory) throws BeansException; }BeanClassLoaderAware/** * 实现此接口既能感知到所属的 ClassLoader */ public interface BeanClassLoaderAware extends Aware { void setBeanClassLoader(ClassLoader classLoader); }BeanNameAware/** * 实现此接口即能感知到所属的 BeanName */ public interface BeanNameAware extends Aware { void setBeanName(String name); }ApplicationContextAware/** * 实现此接口即能感知到所属的 ApplicationContext */ public interface ApplicationContextAware extends Aware { void setApplicationContext(ApplicationContext applicationContext) throws BeansException; }包装处理器根据之前的学习我们知道管理Bean的流程是先通过XML获取对应的配置文件然后在ApplicationContext中执行refresh操作得到beanFactory最后要回归到BeanFacotry中执行创建对象、依赖注入和各种前置、后置操作。在BeanFacotry中我们很容易能获取到相关的BeanNameBeanFactoryBeanClassLoader对象但是无法获取到ApplicationContext对象因为我们是先在ApplicationContext中执行了一些操作再转到BeanFactory中执行剩余的操作。为了解决在BeanFactory中无法获取ApplicationContext对象的问题我们考虑在ApplicationContext中执行操作时把ApplicationContext封装成一个BeanPostProcessor对象直接把封装后的对象加入到BeanFactory中即可。因此就有了下面这个类/** * 包装处理器 * 负责在 Bean 初始化之前把 Spring 的 ApplicationContext 注入到实现了 * ApplicationContextAware 接口的 Bean 中使这些 Bean 能够感知并访问容器本身。 */ public class ApplicationContextAwareProcessor implements BeanPostProcessor { // 要注入的 Spring 应用上下文 private final ApplicationContext applicationContext; public ApplicationContextAwareProcessor(ApplicationContext applicationContext) { this.applicationContext applicationContext; } /** * 在 Bean 初始化init-method、PostConstruct 等之前调用。 * 如果当前 Bean 实现了 ApplicationContextAware 接口就把容器注入进去。 */ Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // 如果bean 实现了 ApplicationContextAware 接口 if (bean instanceof ApplicationContextAware) { // 注入 ApplicationContext让 Bean 可以访问容器资源 ((ApplicationContextAware) bean).setApplicationContext(applicationContext); } return bean; } /** * 在 Bean 初始化之后调用这里不做任何处理直接返回原 Bean。 */ Override public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException { return bean; } }最后由AbstractAutowireCapableBeanFactory.applyBeanPostProcessorsBeforeInitialization方法调用即可。注册BeanPostProcessorOverride public void refresh() throws BeansException { // 1. 子类负责创建/刷新 BeanFactory 并加载 BeanDefinition模板方法 refreshBeanFactory(); // 2. 拿到已经完成定义的 BeanFactory模板方法 ConfigurableListableBeanFactory beanFactory getBeanFactory(); // 3. 添加 ApplicationContextAwareProcessor让继承自 ApplicationContextAware 的 Bean 对象都能感知所属的 ApplicationContext // 由于创建Bean的时候无法获取到对应的ApplicationContext, 因此这里先把ApplicationContext做一个包装, 直接在这里获取 // 其余的Aware接口, 包括BeanFactory BeanName 都可以在创建Bean那块获取 beanFactory.addBeanPostProcessor(new ApplicationContextAwareProcessor(this)); // 4. 先执行 BeanFactoryPostProcessor允许动态修改/补充 BeanDefinition invokeBeanFactoryPostProcessors(beanFactory); // 5. 再把所有 BeanPostProcessor 提前实例化并注册到工厂后续创建的 Bean 才能被拦截 registerBeanPostProcessors(beanFactory); // 6. 一次性提前实例化所有单例 Beanlazy-initfalse 的 beanFactory.preInstantiateSingletons(); }注意这一块就是前文提到的有一些操作先在ApplicationContext中执行在这个环境下执行的时候我直接把当前ApplicationContext对象封装为BeanPostProcessor交给BeanFactory。感知调用操作private Object initializeBean(String beanName, Object bean, BeanDefinition beanDefinition) { // 先检查Aware接口 // invokeAwareMethods if (bean instanceof Aware) { if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(this); } if (bean instanceof BeanClassLoaderAware){ ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader()); } if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } } // 1. 执行 BeanPostProcessor Before 处理 Object wrappedBean applyBeanPostProcessorsBeforeInitialization(bean, beanName); // 执行 Bean 对象的初始化方法 try { invokeInitMethods(beanName, wrappedBean, beanDefinition); } catch (Exception e) { throw new BeansException(Invocation of init method of bean[ beanName ] failed, e); } // 2. 执行 BeanPostProcessor After 处理 wrappedBean applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); return wrappedBean; }这一块在初始化阶段会检查当前Bean是否实现了相应的Aware接口如果有就把相关的对象给到Bean这个在后面的例子中会更好理解。而对于ApplicationContext对象则会在执行BeanPostProcessor的前置操作时执行因为ApplicationContext的封装对象重写了这个方法/** * 在 Bean 初始化init-method、PostConstruct 等之前调用。 * 如果当前 Bean 实现了 ApplicationContextAware 接口就把容器注入进去。 */ Override public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException { // 如果bean 实现了 ApplicationContextAware 接口 if (bean instanceof ApplicationContextAware) { // 注入 ApplicationContext让 Bean 可以访问容器资源 ((ApplicationContextAware) bean).setApplicationContext(applicationContext); } return bean; }测试定义UserService类public class UserService implements BeanNameAware, BeanClassLoaderAware, ApplicationContextAware, BeanFactoryAware { private ApplicationContext applicationContext; private BeanFactory beanFactory; private String uId; private String company; private String location; private UserDao userDao; Override public void setBeanFactory(BeanFactory beanFactory) throws BeansException { this.beanFactory beanFactory; } Override public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext applicationContext; } Override public void setBeanName(String name) { System.out.println(Bean Name is name); } Override public void setBeanClassLoader(ClassLoader classLoader) { System.out.println(ClassLoader classLoader); } public String queryUserInfo() { return userDao.queryUserName(uId) , company , location; } // 其余 get set 方法 }这里的处理方式是首先实现了各个Aware接口同时在Bean中加入了相关字段用来存储获取到的对象。之后重写接口中的相关set方法获取对象将对象地址给到Bean中的相关字段。这也就赋予了Bean获取Spring中相关容器的能力。