spring IOC之篇五 bean的加載
阿新 • • 發佈:2018-05-04
ebean catch har spring容器 ESS oca over con ted
我們終於結束了對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前面顯示的加&
spring IOC之篇五 bean的加載