Spring源码分析-IOC之Aware
2019-04-04 20:54
337 查看
Aware:已感知的,意识到的,所以这些接口从字面意思应该是能感知到所有
Aware前面的含义,Spring中提供了一些Aware相关接口,比如BeanNameAware,BeanFactoryAware,ApplicationContextAware等,下面我们来看下BeanNameAware,BeanNameAware就是将name注入实例,我们来看下具体的应用:
[code]package com.ck.bean; import java.io.Serializable; import org.springframework.beans.factory.BeanNameAware; public class Person implements BeanNameAware, Serializable{ private static final long serialVersionUID = 1L; private String name; @Override public void setBeanName(String name) { this.name = name; } public String getName() { return name; } public void setName(String name) { this.name = name; } }
看下配置文件:
[code]<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.1.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.1.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.1.xsd"> <bean id="person" class="com.ck.bean.Person"/> </beans>
看下测试代码:
[code]package com.ck.ioc; import org.junit.Test; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.support.FileSystemXmlApplicationContext; import com.ck.bean.Person; public class IocTest { @Test public void test() { ConfigurableApplicationContext ctx = new FileSystemXmlApplicationContext( "src/main/resources/applicationContext.xml"); Person person = ctx.getBean(Person.class); System.out.println(person.getName()); } }
我们看下测试结果:
[code]四月 04, 2019 8:31:16 下午 org.springframework.context.support.FileSystemXmlApplicationContext prepareRefresh 信息: Refreshing org.springframework.context.support.FileSystemXmlApplicationContext@3c679bde: startup date [Thu Apr 04 20:31:16 CST 2019]; root of context hierarchy 四月 04, 2019 8:31:17 下午 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions 信息: Loading XML bean definitions from file [X:\CK\stsws\springWeb\src\main\resources\applicationContext.xml] person
我们看到实现了BeanNameAware的接口,最终在实例中获得了在Spring容器中name,spring中的其他aware与BeanNameAware打通小异,各位可以尝试下,我们来看下aware在Spring中的实现原理,我们根据前面介绍的 Spring源码分析-IOC之InitializingBean代码继续往下走:
AbstractAutowireCapableBeanFactory中的initializaBean:
[code]protected Object initializeBean(final String beanName, final Object bean, RootBeanDefinition mbd) { if (System.getSecurityManager() != null) { AccessController.doPrivileged(new PrivilegedAction<Object>() { @Override public Object run() { invokeAwareMethods(beanName, bean); return null; } }, getAccessControlContext()); } else { //注意此入口 invokeAwareMethods(beanName, bean); } Object wrappedBean = bean; if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsBeforeInitialization(wrappedBean, beanName); } try { invokeInitMethods(beanName, wrappedBean, mbd); } catch (Throwable ex) { throw new BeanCreationException( (mbd != null ? mbd.getResourceDescription() : null), beanName, "Invocation of init method failed", ex); } if (mbd == null || !mbd.isSynthetic()) { wrappedBean = applyBeanPostProcessorsAfterInitialization(wrappedBean, beanName); } return wrappedBean; } //此处我们可以看到Aware实现的地方 private void invokeAwareMethods(final String beanName, final Object bean) { if (bean instanceof Aware) { if (bean instanceof BeanNameAware) { ((BeanNameAware) bean).setBeanName(beanName); } if (bean instanceof BeanClassLoaderAware) { ((BeanClassLoaderAware) bean).setBeanClassLoader(getBeanClassLoader()); } if (bean instanceof BeanFactoryAware) { ((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this); } } }
通过代码我们可以一目了然的看到Aware的应用以及在Spring中的应用,这些过程都是在Spring容器实例化bean的地方实现的.
相关文章推荐
- Spring源码分析-IOC之ApplicationContextAwareProcessor
- Spring源码分析(2)-IoC容器的实现
- spring 源码分析--IOC容器初始化一
- spring源码分析之bean之IOC
- Spring源码分析(二)-Spring IoC容器的初始化No.2
- Spring源码分析----IoC容器(二)
- Spring原理与源码分析系列(五)- Spring IoC源码分析(下)
- Spring源码分析1--IoC容器载入Bean定义资源
- Spring源码分析----IOC容器的实现(IoC容器的初始化过程(定位、载入解析、注册))
- 【spring源码分析】BeanDefinitionRegistryPostProcessor接口可自定义bean加入IOC
- Spring IOC 容器源码分析
- spring4.2.9 ioc源码分析(一)——执行refresh之前
- spring 源码分析--IOC容器初始化五
- 转:Spring源码分析:IOC容器在web容器中的启动
- Spring原理与源码分析系列(二)- Spring IoC容器启动过程分析(上)
- Spring IOC源码分析之-刷新前的准备工作
- spring源码学习之路---深度分析IOC容器初始化过程(四)
- Spring Ioc 源码分析(二)- bean的实例化
- Spring IOC源码分析(七):IOC容器的设计实现与bean对象的创建流程
- 做一个合格的程序猿之浅析Spring IoC源码(九)了解BeanNameAware