1 protected Object doCreateBean(final String beanName, final RootBeanDefinition mbd, final Object[] args) { 2 // Instantiate the bean. 3 BeanWrapper instanceWrapper = null; 4 if (mbd.isSingleton()) { 5 instanceWrapper = this.factoryBeanInstanceCache.remove(beanName); 6 } 7 if (instanceWrapper == null) { 8 instanceWrapper = createBeanInstance(beanName, mbd, args); 9 }10 final Object bean = (instanceWrapper != null ? instanceWrapper.getWrappedInstance() : null);11 Class beanType = (instanceWrapper != null ? instanceWrapper.getWrappedClass() : null);12 13 // Allow post-processors to modify the merged bean definition.14 synchronized (mbd.postProcessingLock) {15 if (!mbd.postProcessed) {16 applyMergedBeanDefinitionPostProcessors(mbd, beanType, beanName);17 mbd.postProcessed = true;18 }19 }20 21 // Eagerly cache singletons to be able to resolve circular references22 // even when triggered by lifecycle interfaces like BeanFactoryAware.23 boolean earlySingletonExposure = (mbd.isSingleton() && this.allowCircularReferences &&24 isSingletonCurrentlyInCreation(beanName));25 if (earlySingletonExposure) {26 if (logger.isDebugEnabled()) {27 logger.debug("Eagerly caching bean '" + beanName +28 "' to allow for resolving potential circular references");29 }30 addSingletonFactory(beanName, new ObjectFactory() {31 public Object getObject() throws BeansException {32 return getEarlyBeanReference(beanName, mbd, bean);33 }34 });35 }36 37 // Initialize the bean instance.38 Object exposedObject = bean;39 try {40 populateBean(beanName, mbd, instanceWrapper);41 if (exposedObject != null) {42 exposedObject = initializeBean(beanName, exposedObject, mbd);43 }44 }45 catch (Throwable ex) {46 if (ex instanceof BeanCreationException && beanName.equals(((BeanCreationException) ex).getBeanName())) {47 throw (BeanCreationException) ex;48 }49 else {50 throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Initialization of bean failed", ex);51 }52 }53 54 if (earlySingletonExposure) {55 Object earlySingletonReference = getSingleton(beanName, false);56 if (earlySingletonReference != null) {57 if (exposedObject == bean) {58 exposedObject = earlySingletonReference;59 }60 else if (!this.allowRawInjectionDespiteWrapping && hasDependentBean(beanName)) {61 String[] dependentBeans = getDependentBeans(beanName);62 Set actualDependentBeans = new LinkedHashSet (dependentBeans.length);63 for (String dependentBean : dependentBeans) {64 if (!removeSingletonIfCreatedForTypeCheckOnly(dependentBean)) {65 actualDependentBeans.add(dependentBean);66 }67 }68 if (!actualDependentBeans.isEmpty()) {69 throw new BeanCurrentlyInCreationException(beanName,70 "Bean with name '" + beanName + "' has been injected into other beans [" +71 StringUtils.collectionToCommaDelimitedString(actualDependentBeans) +72 "] in its raw version as part of a circular reference, but has eventually been " +73 "wrapped. This means that said other beans do not use the final version of the " +74 "bean. This is often the result of over-eager type matching - consider using " +75 "'getBeanNamesOfType' with the 'allowEagerInit' flag turned off, for example.");76 }77 }78 }79 }80 81 // Register bean as disposable.82 try {83 registerDisposableBeanIfNecessary(beanName, bean, mbd);84 }85 catch (BeanDefinitionValidationException ex) {86 throw new BeanCreationException(mbd.getResourceDescription(), beanName, "Invalid destruction signature", ex);87 }88 89 return exposedObject;90 }
1 public void setPropertyValues(PropertyValues pvs, boolean ignoreUnknown, boolean ignoreInvalid) 2 throws BeansException { 3 4 List propertyAccessExceptions = null; 5 List propertyValues = (pvs instanceof MutablePropertyValues ? 6 ((MutablePropertyValues) pvs).getPropertyValueList() : Arrays.asList(pvs.getPropertyValues())); 7 for (PropertyValue pv : propertyValues) { 8 try { 9 // This method may throw any BeansException, which won't be caught10 // here, if there is a critical failure such as no matching field.11 // We can attempt to deal only with less serious exceptions.12 setPropertyValue(pv);13 }14 catch (NotWritablePropertyException ex) {15 if (!ignoreUnknown) {16 throw ex;17 }18 // Otherwise, just ignore it and continue...19 }20 catch (NullValueInNestedPathException ex) {21 if (!ignoreInvalid) {22 throw ex;23 }24 // Otherwise, just ignore it and continue...25 }26 catch (PropertyAccessException ex) {27 if (propertyAccessExceptions == null) {28 propertyAccessExceptions = new LinkedList ();29 }30 propertyAccessExceptions.add(ex);31 }32 }33 34 // If we encountered individual exceptions, throw the composite exception.35 if (propertyAccessExceptions != null) {36 PropertyAccessException[] paeArray =37 propertyAccessExceptions.toArray(new PropertyAccessException[propertyAccessExceptions.size()]);38 throw new PropertyBatchUpdateException(paeArray);39 }40 }