mini-spring-为bean填充属性&注入bean

这篇具有很好参考价值的文章主要介绍了mini-spring-为bean填充属性&注入bean。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

bean属性填充的设计

什么是属性填充
属性填充可以在类 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加补全属性方法。
mini-spring-为bean填充属性&注入bean,spring,spring

属性填充要做的事情

1.属性填充要在类实例化创建之后,也就是需要在 AbstractAutowireCapableBeanFactory 的 createBean 方法中添加 applyPropertyValues 操作。由于我们需要在创建Bean时候填充属性操作,那么就需要在 bean 定义 BeanDefinition 类中,添加 PropertyValues 信息。
2.填充属性信息还包括了 Bean 的对象类型(当前bean对其他bean的引用),也就是需要再定义一个 BeanReference,里面其实就是一个简单的 Bean 名称,在具体的实例化操作时进行递归创建和填充。这一步也称为bean注入bean 暂时不考虑循环依赖问题

相关代码
PropertyValue

定义PropertyValue类->该类是一个bean属性的格式

public class PropertyValue {

	private final String name;

	private final Object value;

	public PropertyValue(String name, Object value) {
		this.name = name;
		this.value = value;
	}

	public String getName() {
		return name;
	}

	public Object getValue() {
		return value;
	}
}
PropertyValues

该类是bean属性信息的集合

package org.springframework.beans;

import java.util.ArrayList;
import java.util.List;

/**
 * @author mwan
 */
public class PropertyValues {
	//获取列表
	private final List<PropertyValue> propertyValueList = new ArrayList<>();
	//添加属性
	public void addPropertyValue(PropertyValue pv) {
		propertyValueList.add(pv);
	}
	//这行代码什么含义??
	public PropertyValue[] getPropertyValues() {
		return this.propertyValueList.toArray(new PropertyValue[0]);
	}
	//获取某个属性
	public PropertyValue getPropertyValue(String propertyName) {
		for (int i = 0; i < this.propertyValueList.size(); i++) {
			PropertyValue pv = this.propertyValueList.get(i);
			if (pv.getName().equals(propertyName)) {
				return pv;
			}
		}
		return null;
	}
}

BeanDefinition

bean定义信息补全

/**
 * BeanDefinition实例保存bean的信息,包括class类型、方法构造参数、bean属性、bean的scope等,此处简化只包含class类型和bean属性
 *
 * @author mwan
 */
public class BeanDefinition {

	private Class beanClass;

	private PropertyValues propertyValues;

	public BeanDefinition(Class beanClass) {
		this(beanClass, null);
	}

	public BeanDefinition(Class beanClass, PropertyValues propertyValues) {
		this.beanClass = beanClass;
		this.propertyValues = propertyValues != null ? propertyValues : new PropertyValues();
	}

	public Class getBeanClass() {
		return beanClass;
	}

	public void setBeanClass(Class beanClass) {
		this.beanClass = beanClass;
	}

	public PropertyValues getPropertyValues() {
		return propertyValues;
	}

	public void setPropertyValues(PropertyValues propertyValues) {
		this.propertyValues = propertyValues;
	}
}

AbstractAutowireCapableBeanFactory

主要作用是bean的属性填充
主要关注 createBean 的方法中调用的 applyPropertyValues 方法。
在 applyPropertyValues 中,通过获取 beanDefinition.getPropertyValues() 循环进行属性填充操作,如果遇到的是 BeanReference,那么就需要递归获取 Bean 实例,调用 getBean 方法。文章来源地址https://www.toymoban.com/news/detail-822934.html

/**
 * @author mwan
 */
public abstract class AbstractAutowireCapableBeanFactory extends AbstractBeanFactory {

	private InstantiationStrategy instantiationStrategy = new SimpleInstantiationStrategy();

	@Override
	protected Object createBean(String beanName, BeanDefinition beanDefinition) throws BeansException {
		return doCreateBean(beanName, beanDefinition);
	}

	protected Object doCreateBean(String beanName, BeanDefinition beanDefinition) {
		Object bean = null;
		try {
			bean = createBeanInstance(beanDefinition);
			//为bean填充属性
			applyPropertyValues(beanName, bean, beanDefinition);
		} catch (Exception e) {
			throw new BeansException("Instantiation of bean failed", e);
		}

		addSingleton(beanName, bean);
		return bean;
	}

	/**
	 * 实例化bean
	 *
	 * @param beanDefinition
	 * @return
	 */
	protected Object createBeanInstance(BeanDefinition beanDefinition) {
		return getInstantiationStrategy().instantiate(beanDefinition);
	}

	/**
	 * 为bean填充属性
	 *
	 * @param bean
	 * @param beanDefinition
	 */
	protected void applyPropertyValues(String beanName, Object bean, BeanDefinition beanDefinition) {
		try {
			for (PropertyValue propertyValue : beanDefinition.getPropertyValues().getPropertyValues()) {
				String name = propertyValue.getName();
				Object value = propertyValue.getValue();
				if (value instanceof BeanReference) {
					// beanA依赖beanB,先实例化beanB

					BeanReference beanReference = (BeanReference) value;
					//getBean递归调用 会先create beanB 为其赋值并实例化
					value = getBean(beanReference.getBeanName());
				}

				//通过反射设置属性
				BeanUtil.setFieldValue(bean, name, value);
			}
		} catch (Exception ex) {
			throw new BeansException("Error setting property values for bean: " + beanName, ex);
		}
	}

	public InstantiationStrategy getInstantiationStrategy() {
		return instantiationStrategy;
	}

	public void setInstantiationStrategy(InstantiationStrategy instantiationStrategy) {
		this.instantiationStrategy = instantiationStrategy;
	}
}

到了这里,关于mini-spring-为bean填充属性&注入bean的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 【Spring Boot Bean 注入详解】

    什么是 Bean 注入? 在 Spring 中,Bean 是应用程序的核心构建块。Bean 是由 Spring IoC 容器管理的对象,通过依赖注入实现对象之间的解耦。 Spring Boot 中的 Bean 注入 在 Spring Boot 中,Bean 注入通常通过 @Autowired 、 @Component 、 @Service 、 @Repository 等注解实现。 @Autowired 用于自动装配

    2024年02月21日
    浏览(31)
  • Spring 填充属性和初始化流程源码剖析及扩展实现

    在上一篇博文 讲解 Spring 实例化的不同方式及相关生命周期源码剖析 介绍了 Spring 实例化的不同方式,本文主要围绕实例化过后对象的填充属性和初始化过程进行详细流程剖析 回顾前言知识,doCreateBean-createBeanInstance,通过 Supplier 接口、FactoryMethod、构造函数反射 invoke,创建

    2024年02月06日
    浏览(33)
  • [Spring] @Bean 修饰方法时如何注入参数

    目录 一、@Bean 的简单使用 1、正常情况 2、问题提出 二、解决方案 1、@Qualifier 2、直接写方法名 三、特殊情况 1、DataSource 在开发中,基于 XML 文件配置 Bean 对象的做法非常繁琐且不好维护,因此绝大部分情况下都是使用“完全注解开发”。 对于 Spring 而言,IOC 容器中的 Bean

    2024年01月18日
    浏览(26)
  • Spring——Bean注入几种方式(放入容器)

    个人博客: 全是干货,相信不会让你失望 1.XML方式注入 在现在这个Springboot横行的年代,以XML来注入的方式可能已经不多见了,因为压根用不着,但毕竟是注入方式之一也得提一提,这种方式就是依赖于XML的解析来获取我们需要注入的Bean对象 常见的方式有:set方法注入、构

    2024年02月01日
    浏览(32)
  • 将Bean注入Spring容器的五种方式

    将bean放入Spring容器中有哪些方式? 我们知道平时在开发中使用Spring的时候,都是将对象交由Spring去管理,那么将一个对象加入到Spring容器中,有哪些方式呢,下面我就来总结一下 这种方式其实也是我们最常用的一种方式,@Configuration用来声明一个配置类,然后使用 @Bean 注解

    2024年02月05日
    浏览(24)
  • 4.3---Spring框架之Spring中bean的注入方式---(深入版本)

    Spring基于xml注入bean的几种方式: set()方法注入; 2.构造器注入:①通过index设置参数的位置;②通过type设置参数类型; 静态工厂注入; 实例工厂; Spring IOC注入方式用得最多的是(1)(2)种; 注意:通过Spring创建的对象默认是单例的,如果需要创建多实例对象可以在标签后面添

    2023年04月10日
    浏览(31)
  • 使用@Configuration和@Bean给spring容器中注入组件

    以前我们是使用配置文件来注册bean的,现如今可以用@Configuration 来代替配置文件。

    2024年02月11日
    浏览(27)
  • spring复习:(18)给bean的属性赋值

    类: AbstractAutowireCapableBeanFactory: 其中populateBean用来用我们配置文件里的属性来给bean的属性赋值: 其中applyPropertyValues(beanName, mbd, bw, pvs);真正进行了赋值. 其中调用了bw.setPropertyValues:,代码: setPropertyValues代码: 其中调用了:setPropertyValue(pv);这个方法的代码: 调用setPropertyVal

    2024年02月16日
    浏览(26)
  • 一文道破将bean注入到Spring中的几种方式

    前言: 前两天有学妹问我如何将bean注入到Spring中,虽问题较简单,但还是写此文以告之。 在Java的Spring框架中,将bean注入到容器中是核心概念之一,这是实现依赖注入的基础。Spring提供了多种方式来将bean注入到容器中。 写此文,同时也希望这篇文章能帮助到各位路过的大佬

    2024年04月15日
    浏览(30)
  • B057-spring增强 依赖注入 AOP 代理模式 创建Bean

    DI:依赖注入 环境准备,即写一个spring测试,见工程 构造器注入 即使用构造器来给Bean的对象的属性赋值 MyBean OtherBean SpringTest-Context.xml SpringTest setter方法注入 即用setter方法给bean对象的属性赋值 MyBean OtherBean SpringTest-Context.xml SpringTest AOP 概念 事务管理:比如可以抽取try catch的

    2024年02月12日
    浏览(33)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包