List of usage examples for org.springframework.beans.factory BeanDefinitionStoreException BeanDefinitionStoreException
public BeanDefinitionStoreException(@Nullable String resourceDescription, String beanName, String msg)
From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java
/** * Get the value of a property element. May be a list etc. * Also used for constructor arguments, "propertyName" being null in this case. *//*from w ww .j a va 2s . c o m*/ protected Object parsePropertyValue(Element ele, String beanName, String propertyName) throws BeanDefinitionStoreException { String elementName = (propertyName != null) ? "<property> element for property '" + propertyName + "'" : "<constructor-arg> element"; // Should only have one child element: ref, value, list, etc. NodeList nl = ele.getChildNodes(); Element subElement = null; for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i) instanceof Element) { Element candidateEle = (Element) nl.item(i); if (DESCRIPTION_ELEMENT.equals(candidateEle.getTagName())) { // Keep going: we don't use this value for now. } else { // Child element is what we're looking for. if (subElement != null) { throw new BeanDefinitionStoreException(getResource(), beanName, elementName + " must not contain more than one sub-element"); } subElement = candidateEle; } } } boolean hasRefAttribute = ele.hasAttribute(REF_ATTRIBUTE); boolean hasValueAttribute = ele.hasAttribute(VALUE_ATTRIBUTE); if ((hasRefAttribute && hasValueAttribute) || ((hasRefAttribute || hasValueAttribute)) && subElement != null) { throw new BeanDefinitionStoreException(getResource(), beanName, elementName + " is only allowed to contain either 'ref' attribute OR 'value' attribute OR sub-element"); } if (hasRefAttribute) { String refName = ele.getAttribute(REF_ATTRIBUTE); if (!StringUtils.hasText(refName)) { throw new BeanDefinitionStoreException(getResource(), beanName, elementName + " contains empty 'ref' attribute"); } return new RuntimeBeanReference(refName); } else if (hasValueAttribute) { return ele.getAttribute(VALUE_ATTRIBUTE); } if (subElement == null) { // Neither child element nor "ref" or "value" attribute found. throw new BeanDefinitionStoreException(getResource(), beanName, elementName + " must specify a ref or value"); } return parsePropertySubElement(subElement, beanName); }
From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java
/** * Parse a map element./*from ww w . java2 s . c o m*/ */ protected Map parseMapElement(Element mapEle, String beanName) throws BeanDefinitionStoreException { List entryEles = DomUtils.getChildElementsByTagName(mapEle, ENTRY_ELEMENT); ManagedMap map = new ManagedMap(entryEles.size()); for (Iterator it = entryEles.iterator(); it.hasNext();) { Element entryEle = (Element) it.next(); // Should only have one value child element: ref, value, list, etc. // Optionally, there might be a key child element. NodeList entrySubNodes = entryEle.getChildNodes(); Element keyEle = null; Element valueEle = null; for (int j = 0; j < entrySubNodes.getLength(); j++) { if (entrySubNodes.item(j) instanceof Element) { Element candidateEle = (Element) entrySubNodes.item(j); if (candidateEle.getTagName().equals(KEY_ELEMENT)) { if (keyEle != null) { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element is only allowed to contain one <key> sub-element"); } keyEle = candidateEle; } else { // Child element is what we're looking for. if (valueEle != null) { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element must not contain more than one value sub-element"); } valueEle = candidateEle; } } } // Extract key from attribute or sub-element. Object key = null; boolean hasKeyAttribute = entryEle.hasAttribute(KEY_ATTRIBUTE); boolean hasKeyRefAttribute = entryEle.hasAttribute(KEY_REF_ATTRIBUTE); if ((hasKeyAttribute && hasKeyRefAttribute) || ((hasKeyAttribute || hasKeyRefAttribute)) && keyEle != null) { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element is only allowed to contain either " + "a 'key' attribute OR a 'key-ref' attribute OR a <key> sub-element"); } if (hasKeyAttribute) { key = entryEle.getAttribute(KEY_ATTRIBUTE); } else if (hasKeyRefAttribute) { String refName = entryEle.getAttribute(KEY_REF_ATTRIBUTE); if (!StringUtils.hasText(refName)) { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element contains empty 'key-ref' attribute"); } key = new RuntimeBeanReference(refName); } else if (keyEle != null) { key = parseKeyElement(keyEle, beanName); } else { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element must specify a key"); } // Extract value from attribute or sub-element. Object value = null; boolean hasValueAttribute = entryEle.hasAttribute(VALUE_ATTRIBUTE); boolean hasValueRefAttribute = entryEle.hasAttribute(VALUE_REF_ATTRIBUTE); if ((hasValueAttribute && hasValueRefAttribute) || ((hasValueAttribute || hasValueRefAttribute)) && valueEle != null) { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element is only allowed to contain either " + "'value' attribute OR 'value-ref' attribute OR <value> sub-element"); } if (hasValueAttribute) { value = entryEle.getAttribute(VALUE_ATTRIBUTE); } else if (hasValueRefAttribute) { String refName = entryEle.getAttribute(VALUE_REF_ATTRIBUTE); if (!StringUtils.hasText(refName)) { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element contains empty 'value-ref' attribute"); } value = new RuntimeBeanReference(refName); } else if (valueEle != null) { value = parsePropertySubElement(valueEle, beanName); } else { throw new BeanDefinitionStoreException(getResource(), beanName, "<entry> element must specify a value"); } // Add final key and value to the Map. map.put(key, value); } return map; }
From source file:org.springframework.beans.factory.xml.DefaultXmlBeanDefinitionParser.java
/** * Parse a key sub-element of a map element. *///from w w w .j ava 2s. co m protected Object parseKeyElement(Element keyEle, String beanName) throws BeanDefinitionStoreException { NodeList nl = keyEle.getChildNodes(); Element subElement = null; for (int i = 0; i < nl.getLength(); i++) { if (nl.item(i) instanceof Element) { Element candidateEle = (Element) nl.item(i); // Child element is what we're looking for. if (subElement != null) { throw new BeanDefinitionStoreException(getResource(), beanName, "<key> element must not contain more than one value sub-element"); } subElement = candidateEle; } } return parsePropertySubElement(subElement, beanName); }
From source file:org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.java
/** * Read the given {@link BeanMethod}, registering bean definitions * with the BeanDefinitionRegistry based on its contents. */// www. j a va2 s . com private void loadBeanDefinitionsForBeanMethod(BeanMethod beanMethod) { ConfigurationClass configClass = beanMethod.getConfigurationClass(); MethodMetadata metadata = beanMethod.getMetadata(); String methodName = metadata.getMethodName(); // Do we need to mark the bean as skipped by its condition? if (this.conditionEvaluator.shouldSkip(metadata, ConfigurationPhase.REGISTER_BEAN)) { configClass.skippedBeanMethods.add(methodName); return; } if (configClass.skippedBeanMethods.contains(methodName)) { return; } AnnotationAttributes bean = AnnotationConfigUtils.attributesFor(metadata, Bean.class); Assert.state(bean != null, "No @Bean annotation attributes"); // Consider name and any aliases List<String> names = new ArrayList<>(Arrays.asList(bean.getStringArray("name"))); String beanName = (!names.isEmpty() ? names.remove(0) : methodName); // Register aliases even when overridden for (String alias : names) { this.registry.registerAlias(beanName, alias); } // Has this effectively been overridden before (e.g. via XML)? if (isOverriddenByExistingDefinition(beanMethod, beanName)) { if (beanName.equals(beanMethod.getConfigurationClass().getBeanName())) { throw new BeanDefinitionStoreException( beanMethod.getConfigurationClass().getResource().getDescription(), beanName, "Bean name derived from @Bean method '" + beanMethod.getMetadata().getMethodName() + "' clashes with bean name for containing configuration class; please make those names unique!"); } return; } ConfigurationClassBeanDefinition beanDef = new ConfigurationClassBeanDefinition(configClass, metadata); beanDef.setResource(configClass.getResource()); beanDef.setSource(this.sourceExtractor.extractSource(metadata, configClass.getResource())); if (metadata.isStatic()) { // static @Bean method beanDef.setBeanClassName(configClass.getMetadata().getClassName()); beanDef.setFactoryMethodName(methodName); } else { // instance @Bean method beanDef.setFactoryBeanName(configClass.getBeanName()); beanDef.setUniqueFactoryMethodName(methodName); } beanDef.setAutowireMode(RootBeanDefinition.AUTOWIRE_CONSTRUCTOR); beanDef.setAttribute(RequiredAnnotationBeanPostProcessor.SKIP_REQUIRED_CHECK_ATTRIBUTE, Boolean.TRUE); AnnotationConfigUtils.processCommonDefinitionAnnotations(beanDef, metadata); Autowire autowire = bean.getEnum("autowire"); if (autowire.isAutowire()) { beanDef.setAutowireMode(autowire.value()); } String initMethodName = bean.getString("initMethod"); if (StringUtils.hasText(initMethodName)) { beanDef.setInitMethodName(initMethodName); } String destroyMethodName = bean.getString("destroyMethod"); beanDef.setDestroyMethodName(destroyMethodName); // Consider scoping ScopedProxyMode proxyMode = ScopedProxyMode.NO; AnnotationAttributes attributes = AnnotationConfigUtils.attributesFor(metadata, Scope.class); if (attributes != null) { beanDef.setScope(attributes.getString("value")); proxyMode = attributes.getEnum("proxyMode"); if (proxyMode == ScopedProxyMode.DEFAULT) { proxyMode = ScopedProxyMode.NO; } } // Replace the original bean definition with the target one, if necessary BeanDefinition beanDefToRegister = beanDef; if (proxyMode != ScopedProxyMode.NO) { BeanDefinitionHolder proxyDef = ScopedProxyCreator.createScopedProxy( new BeanDefinitionHolder(beanDef, beanName), this.registry, proxyMode == ScopedProxyMode.TARGET_CLASS); beanDefToRegister = new ConfigurationClassBeanDefinition( (RootBeanDefinition) proxyDef.getBeanDefinition(), configClass, metadata); } if (logger.isDebugEnabled()) { logger.debug(String.format("Registering bean definition for @Bean method %s.%s()", configClass.getMetadata().getClassName(), beanName)); } this.registry.registerBeanDefinition(beanName, beanDefToRegister); }
From source file:org.springframework.context.annotation.ConfigurationClassBeanDefinitionReader.java
protected boolean isOverriddenByExistingDefinition(BeanMethod beanMethod, String beanName) { if (!this.registry.containsBeanDefinition(beanName)) { return false; }//w ww.j a v a 2 s . c o m BeanDefinition existingBeanDef = this.registry.getBeanDefinition(beanName); // Is the existing bean definition one that was created from a configuration class? // -> allow the current bean method to override, since both are at second-pass level. // However, if the bean method is an overloaded case on the same configuration class, // preserve the existing bean definition. if (existingBeanDef instanceof ConfigurationClassBeanDefinition) { ConfigurationClassBeanDefinition ccbd = (ConfigurationClassBeanDefinition) existingBeanDef; return ccbd.getMetadata().getClassName() .equals(beanMethod.getConfigurationClass().getMetadata().getClassName()); } // A bean definition resulting from a component scan can be silently overridden // by an @Bean method, as of 4.2... if (existingBeanDef instanceof ScannedGenericBeanDefinition) { return false; } // Has the existing bean definition bean marked as a framework-generated bean? // -> allow the current bean method to override it, since it is application-level if (existingBeanDef.getRole() > BeanDefinition.ROLE_APPLICATION) { return false; } // At this point, it's a top-level override (probably XML), just having been parsed // before configuration class processing kicks in... if (this.registry instanceof DefaultListableBeanFactory && !((DefaultListableBeanFactory) this.registry).isAllowBeanDefinitionOverriding()) { throw new BeanDefinitionStoreException( beanMethod.getConfigurationClass().getResource().getDescription(), beanName, "@Bean definition illegally overridden by existing bean definition: " + existingBeanDef); } if (logger.isInfoEnabled()) { logger.info(String.format( "Skipping bean definition for %s: a definition for bean '%s' " + "already exists. This top-level bean definition is considered as an override.", beanMethod, beanName)); } return true; }