我们终于结束了对XML配置文件的解析,接下来我们要分析bean的加载,即对代码getBean的分析:
People p = (People)ctx.getBean("cutesource");
@SuppressWarnings("unchecked") protected <T> T doGetBean( final String name, final Class<T> requiredType, final Object[] args, boolean typeCheckOnly) throws BeansException { // 根据名称获取对应的beanName。如果传入的别名A指向bean B则返回bean B的beanName,如果别名A指向别名B 然后别名B指向bean c 则返回bean C,如果传入的是FactoryBean,则则去掉修饰符 ,比如 name=”&aa” 则变为 name=”aa” final String beanName = transformedBeanName(name); Object bean; // Eagerly check singleton cache for manually registered singletons. // 尝试从缓存中加载单例。首次从单例缓存中加载,如果不成功则从singletonFactories中加载。因为在创建单例的时候存在着循环依赖,所以不等bean创建完成就就将创建bean的ObjectFactory提早曝光加入到缓存中,一旦下一个bean依赖上一个bean 则直接使用ObjectFactory 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"); } else { logger.debug("Returning cached instance of singleton bean ‘" + beanName + "‘"); } } // 返回对应的实例,缓存中记录的只是bean最初始化的状态,不一定是最终的bean,有时间需要返回时的指定方法实例化的实例,例如 FactoryBean的getObject的方法的实例 bean = getObjectForBeanInstance(sharedInstance, name, beanName, null); } else { // Fail if we‘re already creating this bean instance: // We‘re assumably within a circular reference. // 只有在单例的情况下面才去解决循环依赖,如果原型的情况下面则直接抛出异常 if (isPrototypeCurrentlyInCreation(beanName)) { throw new BeanCurrentlyInCreationException(beanName); } // Check if bean definition exists in this factory. BeanFactory parentBeanFactory = getParentBeanFactory(); if (parentBeanFactory != null && !containsBeanDefinition(beanName)) { // Not found -> check parent. // 如果parentBeanFactory 不为空,且当前的beanFactory不包含beanName则递归到他的上一层去加载beanName String nameToLookup = originalBeanName(name); if (args != null) { // Delegation to parent with explicit args. // args为传入的构造函数的参数 return (T) parentBeanFactory.getBean(nameToLookup, args); } else { // 没有构造参数则使用标准的方法获取bean // No args -> delegate to standard getBean method. return parentBeanFactory.getBean(nameToLookup, requiredType); } } if (!typeCheckOnly) { markBeanAsCreated(beanName); } try { // 将GenericBeanDefinition转换为 RootBeanDefinition final RootBeanDefinition mbd = getMergedLocalBeanDefinition(beanName); // 如果父类bean不为空的话,合并一下父类的属性 checkMergedBeanDefinition(mbd, beanName, args); // Guarantee initialization of beans that the current bean depends on. String[] dependsOn = mbd.getDependsOn(); if (dependsOn != null) { // 获取bean的其他依赖,如果依赖其他的bean,则先获取其他的bean for (String dependsOnBean : dependsOn) { if (isDependent(beanName, dependsOnBean)) { throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Circular depends-on relationship between ‘" + beanName + "‘ and ‘" + dependsOnBean + "‘"); } // 把该bean依赖的其他的bean加入到dependentBeanMap集合中去 registerDependentBean(dependsOnBean, beanName); // 获取其依赖的其他bean getBean(dependsOnBean); } } // Create bean instance. // 根据不同的scope进行初始化策略 if (mbd.isSingleton()) { sharedInstance = getSingleton(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { try { return createBean(beanName, mbd, args); } catch (BeansException ex) { // Explicitly remove instance from singleton cache: It might have been put there // eagerly by the creation process, to allow for circular reference resolution. // Also remove any beans that received a temporary reference to the bean. destroySingleton(beanName); throw ex; } } }); bean = getObjectForBeanInstance(sharedInstance, name, beanName, mbd); } else if (mbd.isPrototype()) { // It‘s a prototype -> create a new instance. Object prototypeInstance = null; try { beforePrototypeCreation(beanName); prototypeInstance = createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } bean = getObjectForBeanInstance(prototypeInstance, name, beanName, mbd); } else { String scopeName = mbd.getScope(); final Scope scope = this.scopes.get(scopeName); if (scope == null) { throw new IllegalStateException("No Scope registered for scope name ‘" + scopeName + "‘"); } try { Object scopedInstance = scope.get(beanName, new ObjectFactory<Object>() { @Override public Object getObject() throws BeansException { beforePrototypeCreation(beanName); try { return createBean(beanName, mbd, args); } finally { afterPrototypeCreation(beanName); } } }); bean = getObjectForBeanInstance(scopedInstance, name, beanName, mbd); } catch (IllegalStateException ex) { throw new BeanCreationException(beanName, "Scope ‘" + scopeName + "‘ is not active for the current thread; consider " + "defining a scoped proxy for this bean if you intend to refer to it from a singleton", ex); } } } catch (BeansException ex) { cleanupAfterBeanCreationFailure(beanName); throw ex; } } // 根据传入的参数 requiredType和返回的bean类型不一致的时候,进行装换 // Check if required type matches the type of the actual bean instance. if (requiredType != null && bean != null && !requiredType.isAssignableFrom(bean.getClass())) { try { return getTypeConverter().convertIfNecessary(bean, requiredType); } catch (TypeMismatchException ex) { if (logger.isDebugEnabled()) { logger.debug("Failed to convert bean ‘" + name + "‘ to required type [" + ClassUtils.getQualifiedName(requiredType) + "]", ex); } throw new BeanNotOfRequiredTypeException(name, requiredType, bean.getClass()); } } return (T) bean; }
进一步分析 FactoryBean的使用
FactoryBean接口对于Spring框架来说占有重要的地位,Spring自身就提供了70多种实现类。他们隐藏了一下实例化的复杂的细节,给上层应用带来便利。
public interface FactoryBean<T> { // 返回由FactoryBean创建的Bean实例,如果isSingleton()方法返回的是true,则该实例会放入到Spring容器的单例缓存池当中 T getObject() throws Exception; // 返回FactoryBean创建的bean类型 Class<?> getObjectType(); // 返回由factoryBean创建实例的Bean是singleton还是prototype boolean isSingleton(); }
创建一个实体用Factory进行实例化
public class People { private String id; private String name; private Integer age; public String getId() { return id; }
创建一个自定义的FactoryBean方法
public class PeopleFactoryBean implements FactoryBean<People> { private String peopleInfo; public People getObject() throws Exception { People people = new People(); String[] infos = peopleInfo.split(","); people.setId(infos[0]); people.setName(infos[1]); people.setAge(Integer.parseInt(infos[2])); return people; } public Class<People> getObjectType() { return People.class; } public boolean isSingleton() { return false; } public String getPeopleInfo() { return peopleInfo; } public void setPeopleInfo(String peopleInfo) { this.peopleInfo = peopleInfo; } }
配置文件中配置:
<bean id="people" class="com.spring.test.factory.PeopleFactoryBean"> <property name="peopleInfo" value="gh2,address2,22"></property> </bean>
即可使用自定义的逻辑进行实例化,当调用getBean的时候,Spring通过反射机制发现PeopleFactoryBean实现了FactoryBean,这时候Spring容器就调用接口方法PeopleFactoryBean#getObject()方法返回。如果希望获取PeopleFactoryBean实例,则在beanName前面显示的加&
原文地址:https://www.cnblogs.com/histlyb/p/8988808.html
时间: 2024-10-08 06:05:36