Spring源码分析-IOC之Aware

Aware:已感知的,意识到的,所以这些接口从字面意思应该是能感知到所有Aware前面的含义,Spring中提供了一些Aware相关接口,比如BeanNameAware,BeanFactoryAware,ApplicationContextAware等,下面我们来看下BeanNameAware,BeanNameAware就是将name注入实例,我们来看下具体的应用:

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;
	}
	
}

看下配置文件:

<?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>

看下测试代码:

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());
	}

}

我们看下测试结果:

四月 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:

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的地方实现的.

猜你喜欢

转载自blog.csdn.net/cgsyck/article/details/89035046