首页 > 编程语言 >Spring源码-initializeBean

Spring源码-initializeBean

时间:2022-10-01 19:33:07浏览次数:54  
标签:Object mbd Spring beanName bean 源码 result null initializeBean

initializeBean

protected Object initializeBean(String beanName, Object bean, @Nullable RootBeanDefinition mbd) {
	if (System.getSecurityManager() != null) {
		AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
			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;
}
  1. initializeBean方法调用invokeAwareMethods为实现了BeanNameAware,BeanClassLoaderAware,BeanFactoryAware的接口注入相应的bean。

  2. 调用BeanPostProcessor的postProcessBeforeInitialization。

  3. 调用init方法

  4. 调用BeanPostProcessor的postProcessAfterInitialization。

    private void invokeAwareMethods(String beanName, Object bean) {
     if (bean instanceof Aware) {
     	if (bean instanceof BeanNameAware) {
     		((BeanNameAware) bean).setBeanName(beanName);
     	}
     	if (bean instanceof BeanClassLoaderAware) {
     		ClassLoader bcl = getBeanClassLoader();
     		if (bcl != null) {
     			((BeanClassLoaderAware) bean).setBeanClassLoader(bcl);
     		}
     	}
     	if (bean instanceof BeanFactoryAware) {
     		((BeanFactoryAware) bean).setBeanFactory(AbstractAutowireCapableBeanFactory.this);
     	}
     }
    

    }

initializeBean方法调用invokeAwareMethods为实现了BeanNameAware,BeanClassLoaderAware,BeanFactoryAware的接口注入相应的属性。

@Override
public Object applyBeanPostProcessorsBeforeInitialization(Object existingBean, String beanName)
		throws BeansException {

	Object result = existingBean;
	for (BeanPostProcessor processor : getBeanPostProcessors()) {
		Object current = processor.postProcessBeforeInitialization(result, beanName);
		if (current == null) {
			return result;
		}
		result = current;
	}
	return result;
}

调用BeanPostProcessor的postProcessBeforeInitialization。

protected void invokeInitMethods(String beanName, Object bean, @Nullable RootBeanDefinition mbd)
		throws Throwable {

	boolean isInitializingBean = (bean instanceof InitializingBean);
	if (isInitializingBean && (mbd == null || !mbd.isExternallyManagedInitMethod("afterPropertiesSet"))) {
		if (logger.isTraceEnabled()) {
			logger.trace("Invoking afterPropertiesSet() on bean with name '" + beanName + "'");
		}
		if (System.getSecurityManager() != null) {
			try {
				AccessController.doPrivileged((PrivilegedExceptionAction<Object>) () -> {
					((InitializingBean) bean).afterPropertiesSet();
					return null;
				}, getAccessControlContext());
			}
			catch (PrivilegedActionException pae) {
				throw pae.getException();
			}
		}
		else {
			((InitializingBean) bean).afterPropertiesSet();
		}
	}

	if (mbd != null && bean.getClass() != NullBean.class) {
		String initMethodName = mbd.getInitMethodName();
		if (StringUtils.hasLength(initMethodName) &&
				!(isInitializingBean && "afterPropertiesSet".equals(initMethodName)) &&
				!mbd.isExternallyManagedInitMethod(initMethodName)) {
			invokeCustomInitMethod(beanName, bean, mbd);
		}
	}
}

调用init方法,先调用InitializingBean的afterPropertiesSet方法,再调用自定义的init方法。

@Override
public Object applyBeanPostProcessorsAfterInitialization(Object existingBean, String beanName)
		throws BeansException {

	Object result = existingBean;
	for (BeanPostProcessor processor : getBeanPostProcessors()) {
		Object current = processor.postProcessAfterInitialization(result, beanName);
		if (current == null) {
			return result;
		}
		result = current;
	}
	return result;
}

调用BeanPostProcessor 的postProcessAfterInitialization方法。

标签:Object,mbd,Spring,beanName,bean,源码,result,null,initializeBean
From: https://www.cnblogs.com/shigongp/p/16747627.html

相关文章

  • spring-dao.xml
    <?xmlversion="1.0"encoding="UTF-8"?><beansxmlns="http://www.springframework.org/schema/beans"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"......
  • openGauss源码安装
    openGauss源码安装参考官方文档:Compilation|openGauss平台配置:操作系统:CentOS7.6处理器数:2每个处理器的核数:2运行内存:8G磁盘内存:200G1.创建omm用户groupaddo......
  • Spring源码-applyPropertyValues
    applyPropertyValuesprotectedvoidapplyPropertyValues(StringbeanName,BeanDefinitionmbd,BeanWrapperbw,PropertyValuespvs){ if(pvs.isEmpty()){ ret......
  • spring boot 使用jetty作为内置服务器
    <dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><exclusions><exclusion><groupId>......
  • 002.Spring IOC的原理——包括:Spring IOC简介——Spring IOC的装配流程——Spring Be
    1.5SpringIOC的原理1.5.1SpringIOC简介Spring通过一个配置文件描述Bean与Bean之间的依赖关系,利用Java的反射功能实例化Bean并建立Bean之间的关系。Spring的IOC容器......
  • Netty 学习(六):创建 NioEventLoopGroup 的核心源码说明
    Netty学习(六):创建NioEventLoopGroup的核心源码说明作者:Grey原文地址:博客园:Netty学习(六):创建NioEventLoopGroup的核心源码说明CSDN:Netty学习(六):创建NioEventLoopG......
  • Spring源码-autowireByType
    autowireByTypeprotectedvoidautowireByType( StringbeanName,AbstractBeanDefinitionmbd,BeanWrapperbw,MutablePropertyValuespvs){ TypeConverterconve......
  • 集合框架——LinkedList集合源码分析
    目录示例代码底层代码第1步(初始化集合)第2步(往集合中添加一个元素)第3步(往集合中添加第二个元素)第4步(往集合中添加第三个元素)LinkedList添加元素流程示意图第5步(删除集合中第......
  • Spring MVC入门(十一):注解配置SpringMVC
    构建1个maven项目导入依赖<packaging>war</packaging><dependencies><!--SpringMVC--><dependency><groupId>org.springframework</......
  • Spring MVC入门(十):异常处理
    异常处理简介SpringMVC提供了一个处理控制器方法执行过程中所出现的异常的接口:HandlerExceptionResolverHandlerExceptionResolver接口的实现类有:DefaultHandlerExceptionRe......