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
解析ref引用对象
转换成代码
//我们知道foo.bar.xxx 被解析为一个beanDefiniton,假设为xxxBeanDefinition
reference = new RuntimeBeanReference("otherBeanName");
xxxBeanDefinition.getPropertyValues().addPropertyValue("referBeanName", reference);
参考:
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
inner1
6
参考:
https://www.cnblogs.com/EasonJim/p/6883719.html
3.list,map,set(集合)
https://blog.csdn.net/l1585931143/article/details/72629348