1、BeanNameAware简介
实现该接口的bean会意识到自己在beanfactory的的名字,该接口只有一个方法setBeanName。
public interface BeanNameAware extends Aware {
void setBeanName(String name);
}
2、实例
修改上面的实例,增加UserBeanFactoryAware
2.1、UserBeanNameAware类
public class UserBeanNameAware implements BeanNameAware {
@Override
public void setBeanName(String name) {
System.out.println("my name is "+name);
}
}
2.2、配置文件增加
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<bean id="user" class="com.vesus.spring.model.User" />
<bean id="userbeanname" class="com.vesus.spring.factory.UserBeanNameAware" />
</beans>
2.3、测试类
public class Test {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml") ;
UserBeanNameAware aware = (UserBeanNameAware) context.getBean("userbeanname");
}
}
2.4、测试结果
my name is userbeanname
3、源码分析
3.1、入口方法doGetBean()
protected <T> T doGetBean(final String name, @Nullable final Class<T> requiredType,
@Nullable final Object[] args, boolean typeCheckOnly) throws BeansException {
final String beanName = transformedBeanName(name);
Object bean;
......
//如果是单例
if (mbd.isSingleton()) {
sharedInstance = getSingleton(beanName, () -> {
try {
//创建bean
return createBean(beanName, mbd, args);
}
}
}
.....
return (T) bean;
}
继续跟踪得到BeanFactoryAware的调用方法initializeBean
3.2、initializeBean注册方法解析
protected Object initializeBean(final String beanName, final Object bean, @Nullable RootBeanDefinition mbd) {
if (System.getSecurityManager() != null) {
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
invokeAwareMethods(beanName, bean);
return null;
}, getAccessControlContext());
}
else {
//判断bean实例的类型,并执行相应的方法
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;
}
继续跟踪invokeAwareMethods可以看到BeanFactoryAware实现类的调用。
3.2、invokeAwareMethods方法解析
private void invokeAwareMethods(final String beanName, final Object bean) {
if (bean instanceof Aware) {
//判断是否为BeanNameAware实例,如果是,则调用setBeanName方法
if (bean instanceof BeanNameAware) {
((BeanNameAware) bean).setBeanName(beanName);
}
if (bean instanceof BeanClassLoaderAware) {
ClassLoader bcl = getBeanClassLoader();
if (bcl != null) {
((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
}
}
//判断是否为BeanFactoryAware实例,如果是,则调用setBeanFactory方法
if (bean instanceof BeanFactoryAware) {
((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
}
}
}
至此,BeanNameAware的源码分析完成。