本文主要是介绍Spring Framework(十四)Spring Framework 获取 Bean 之 一探究竟,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
GetBeanByClass
public <T> T getBean(Class<T> requiredType, Object... args) throws BeansException {// 1. 解析为namedBeanNamedBeanHolder<T> namedBean = resolveNamedBean(requiredType, args);if (namedBean != null) {return namedBean.getBeanInstance();}// 2. 获取父BeanFactory,然后从父BeanFactory获取BeanBeanFactory parent = getParentBeanFactory();if (parent != null) {return parent.getBean(requiredType, args);}// 3. 还是找不到的话,抛出异常。throw new NoSuchBeanDefinitionException(requiredType);}
在这里面可以看到实际的他和类加载机制不一样,而是优先从本地获取Bean。所以这里是可以简单的理解为,存在这么以下的场景的时候比较合适的,父工厂里面存放一些commonbean,然后子工厂可以根据自己的实际需求去覆盖。
下面详细看一下。
resolveNamedBean
private <T> NamedBeanHolder<T> resolveNamedBean(Class<T> requiredType, Object... args) throws BeansException {Assert.notNull(requiredType, "Required type must not be null");// 1. 获取候选者的bean名称String[] candidateNames = getBeanNamesForType(requiredType);// 2. if (candidateNames.length > 1) {List<String> autowireCandidates = new ArrayList<String>(candidateNames.length);for (String beanName : candidateNames) {if (!containsBeanDefinition(beanName) || getBeanDefinition(beanName).isAutowireCandidate()) {autowireCandidates.add(beanName);}}if (!autowireCandidates.isEmpty()) {candidateNames = autowireCandidates.toArray(new String[autowireCandidates.size()]);}}if (candidateNames.length == 1) {String beanName = candidateNames[0];return new NamedBeanHolder<T>(beanName, getBean(beanName, requiredType, args));}else if (candidateNames.length > 1) {Map<String, Object> candidates = new LinkedHashMap<String, Object>(candidateNames.length);for (String beanName : candidateNames) {if (containsSingleton(beanName)) {candidates.put(beanName, getBean(beanName, requiredType, args));}else {candidates.put(beanName, getType(beanName));}}String candidateName = determinePrimaryCandidate(candidates, requiredType);if (candidateName == null) {candidateName = determineHighestPriorityCandidate(candidates, requiredType);}if (candidateName != null) {Object beanInstance = candidates.get(candidateName);if (beanInstance instanceof Class) {beanInstance = getBean(candidateName, requiredType, args);}return new NamedBeanHolder<T>(candidateName, (T) beanInstance);}throw new NoUniqueBeanDefinitionException(requiredType, candidates.keySet());}return null;}
- 获取这个Class类型的所有的Bean名称数组。
- 如果候选者只有一个的话,直接返回。
- 如果存在多个Bean,是否指定了PrimaryBean(@Primary),就返回Primary,
- 否则继续按照优先级进行排序,还是搞不出来的话抛出异常NoUniqueBeanDefinitionException。
- 正常会返回一个NamedHBeanHodler,然后执行整个getBean流程。这个方式根据名称获取Bean的方法。所以这也是为什么叫做NamedBean了。
public class NamedBeanHolder<T> implements NamedBean {private final String beanName;private final T beanInstance;}
下面看看这个方法。
GetBeanByName
@Overridepublic Object getBean(String name) throws BeansException {return doGetBean(name, null, null, false);}@Overridepublic <T> T getBean(String name, Class<T> requiredType) throws BeansException {return doGetBean(name, requiredType, null, false);}@Overridepublic Object getBean(String name, Object... args) throws BeansException {return doGetBean(name, null, args, false);}public <T> T getBean(String name, Class<T> requiredType, Object... args) throws BeansException {return doGetBean(name, requiredType, args, false);}
可以看到这里是直接调用了doGetBean
方法。
doGetBean 真正执行Bean的方法
这里有个有意思的东西,在Spring里面在真正的执行逻辑的方法基本上都叫做doXxx。
protected <T> T doGetBean(final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly)throws BeansException {final String beanName = transformedBeanName(name);Object bean;// Eagerly check singleton cache for manually registered singletons.Object sharedInstance = getSingleton(beanName);if (sharedInstance != null && args == null) {if (logger.isDebugEnabled()) {if (isSingletonCurrentlyInCreation(beanName)) {logger.debug("Returning eagerly cached instance of singleton bean '" + beanName +"' that is not fully initialized yet - a consequence of a circular reference");}
这篇关于Spring Framework(十四)Spring Framework 获取 Bean 之 一探究竟的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!