ParentBeanFactory解析

编程

public interface HierarchicalBeanFactory extends BeanFactory {

/**

* Return the parent bean factory, or {@code null} if there is none.

*/

@Nullable

// 获取父工厂

BeanFactory getParentBeanFactory();

/**

* Return whether the local bean factory contains a bean of the given name,

* ignoring beans defined in ancestor contexts.

* <p>This is an alternative to {@code containsBean}, ignoring a bean

* of the given name from an ancestor bean factory.

* @param name the name of the bean to query

* @return whether a bean with the given name is defined in the local factory

* @see BeanFactory#containsBean

*/

boolean containsLocalBean(String name);

}

子接口ConfigurableBeanFactory包含了setParentBeanFactory方法

应用

// Check if bean definition exists in this factory.

//doGetBean 方法中获取bean首先从父工厂获取,没有了才自己创建bean

BeanFactory parentBeanFactory = getParentBeanFactory();

if (parentBeanFactory != null && !containsBeanDefinition(beanName)) {

// Not found -> check parent.

String nameToLookup = originalBeanName(name);

if (parentBeanFactory instanceof AbstractBeanFactory) {

return ((AbstractBeanFactory) parentBeanFactory).doGetBean(

nameToLookup, requiredType, args, typeCheckOnly);

}

else if (args != null) {

// Delegation to parent with explicit args.

return (T) parentBeanFactory.getBean(nameToLookup, args);

}

else if (requiredType != null) {

// No args -> delegate to standard getBean method.

return parentBeanFactory.getBean(nameToLookup, requiredType);

}

else {

return (T) parentBeanFactory.getBean(nameToLookup);

}

}

其他

spring父子容器问题

https://juejin.im/post/5c9de80251882567c74c76e6

以上是 ParentBeanFactory解析 的全部内容, 来源链接: utcz.com/z/515203.html

回到顶部