Spring Bean的解析 BeanDefinition 与 RuntimeBeanReference

标签: Spring-Bean生命周期  Spring-IOC&注入&注解&扫描

1.BeanDefinitionHolder

持有name和aliases,为注册做准备

/**
 * Holder for a BeanDefinition with name and aliases.
 * Can be registered as a placeholder for an inner bean.
 */
public class BeanDefinitionHolder implements BeanMetadataElement {
    private final BeanDefinition beanDefinition;

    private final String beanName;

    @Nullable
    private final String[] aliases;
}

2. BeanDefinitionReaderUtils

配合注册时配合BeanDefinitionHolder来注册

public class BeanDefinitionReaderUtils {
    public static void registerBeanDefinition(
            BeanDefinitionHolder definitionHolder, BeanDefinitionRegistry registry)
            throws BeanDefinitionStoreException {

        // Register bean definition under primary name.
        String beanName = definitionHolder.getBeanName();
        registry.registerBeanDefinition(beanName, definitionHolder.getBeanDefinition());

        // Register aliases for bean name, if any.
        String[] aliases = definitionHolder.getAliases();
        if (aliases != null) {
            for (String alias : aliases) {
                registry.registerAlias(beanName, alias);
            }
        }
    }
}

3.BeanDefinitionCustomizer

提供一个接口定制化BeanDefinition

public interface BeanDefinitionCustomizer {

    /**
     * Customize the given bean definition.
     */
    void customize(BeanDefinition bd);
}

示例:

@Test
    public void beanClassWithSimpleProperty() {
        String[] dependsOn = new String[] { "A", "B", "C" };
        BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
        bdb.setScope(BeanDefinition.SCOPE_PROTOTYPE);
        bdb.addPropertyReference("age", "15");
        for (int i = 0; i < dependsOn.length; i++) {
            bdb.addDependsOn(dependsOn[i]);
        }

        bdb.applyCustomizers(new BeanDefinitionCustomizer()
        {

            @Override
            public void customize(BeanDefinition bd) {
                bdb.addPropertyReference("age", "16");
            }
        });

        RootBeanDefinition rbd = (RootBeanDefinition) bdb.getBeanDefinition();
        assertFalse(rbd.isSingleton());
        assertEquals(TestBean.class, rbd.getBeanClass());
        assertTrue("Depends on was added", Arrays.equals(dependsOn, rbd.getDependsOn()));
        assertTrue(rbd.getPropertyValues().contains("age"));
    }

4.BeanDefinitionValueResolver

对BeanDefinition中的propertyValues进行解析

1. RuntimeBeanReference

<bean class="foo.bar.xxx">
   <property name="referBeanName" ref="otherBeanName" />
</bean>

在Spring的解析段,其实容器中是没有依赖的Bean的实例的因此,那么这是这个被依赖的Bean如何在BeanDefinition中表示呢?
答案就是RuntimeBeanReference,在解析到依赖的Bean的时侯,解析器会依据依赖bean的name创建一个RuntimeBeanReference对像,将这个对像放入BeanDefinition的MutablePropertyValues中。

转换成代码

//我们知道foo.bar.xxx 被解析为一个beanDefiniton,假设为xxxBeanDefinition
reference = new RuntimeBeanReference("otherBeanName");
xxxBeanDefinition.getPropertyValues().addPropertyValue("referBeanName", reference);

在创建Bean时,需要将依赖解析成真正的在Spring容器中存在的Bean。这是在getBean时由AbstractAutowireCapableBeanFactory在applyPropertyValues方法中通过BeanDefinitionValueResolver来实现的。BeanDefinitionValueResolver将真正的依赖bean和referBeanName关联起来。

参考:
https://blog.csdn.net/jerryai1/article/details/52980239

到了BeanDefinitionValueResolver会对不同的value类型进行解析,最终还是调用了beanFactory的getBean方法获取

    public Object resolveValueIfNecessary(Object argName, @Nullable Object value) {
        // We must check each value to see whether it requires a runtime reference
        // to another bean to be resolved.
        if (value instanceof RuntimeBeanReference) {
            RuntimeBeanReference ref = (RuntimeBeanReference) value;
            return resolveReference(argName, ref);
        }
...
}

    private Object resolveReference(Object argName, RuntimeBeanReference ref) {
        try {
            Object bean;
            String refName = ref.getBeanName();
            refName = String.valueOf(doEvaluate(refName));
            if (ref.isToParent()) {
                if (this.beanFactory.getParentBeanFactory() == null) {
                    throw new BeanCreationException(
                            this.beanDefinition.getResourceDescription(), this.beanName,
                            "Can't resolve reference to bean '" + refName +
                            "' in parent factory: no parent factory available");
                }
                bean = this.beanFactory.getParentBeanFactory().getBean(refName);
            }
            else {
                bean = this.beanFactory.getBean(refName);
                this.beanFactory.registerDependentBean(refName, this.beanName);
            }
            if (bean instanceof NullBean) {
                bean = null;
            }
            return bean;
        }
        catch (BeansException ex) {
            throw new BeanCreationException(
                    this.beanDefinition.getResourceDescription(), this.beanName,
                    "Cannot resolve reference to bean '" + ref.getBeanName() + "' while setting " + argName, ex);
        }
    }

测试代码

    @Test
    public void propertyReferenceTest() {
        DefaultListableBeanFactory beanFactory=new DefaultListableBeanFactory();

        {
            BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(Colour.class);
            beanFactory.registerBeanDefinition("colour",bdb.getBeanDefinition());
        }

        {
            BeanDefinitionBuilder bdb = BeanDefinitionBuilder.rootBeanDefinition(TestBean.class);
            bdb.addPropertyReference("favouriteColour","colour");
            beanFactory.registerBeanDefinition("testBean",bdb.getBeanDefinition());
        }

        TestBean bean=beanFactory.getBean(TestBean.class);
    }

2.innerBean

innerBean最终会解析成BeanDefinitionHolder

    <bean id="hasInnerBeansForConstructor" class="org.springframework.tests.sample.beans.TestBean">
        <constructor-arg>
            <bean id="innerBean" class="org.springframework.tests.sample.beans.TestBean" destroy-method="destroy">
                <constructor-arg><value>inner1</value></constructor-arg>
                <constructor-arg type="int"><value>6</value></constructor-arg>
            </bean>
        </constructor-arg>
    </bean>

参考:
https://www.cnblogs.com/EasonJim/p/6883719.html

 

版权声明:本文为z69183787原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/z69183787/article/details/104542990