/** * Register bean definitions contained in a Map. Ignore ineligible properties. * * @param m Map name -> property (String or Object). Property values will be strings if coming * from a Properties file etc. Property names (keys) <b>must</b> be strings. Class keys must * be Strings. * @param prefix The match or filter within the keys in the map: e.g. 'beans.' * @param resourceDescription description of the resource that the Map came from (for logging * purposes) * @return the number of bean definitions found * @throws BeansException in case of loading or parsing errors * @see #registerBeanDefinitions(Map, String) */ public int registerBeanDefinitions(Map m, String prefix, String resourceDescription) throws BeansException { if (prefix == null) { prefix = ""; } int beanCount = 0; Set keys = m.keySet(); Iterator itr = keys.iterator(); while (itr.hasNext()) { String key = (String) itr.next(); if (key.startsWith(prefix)) { // Key is of form prefix<name>.property String nameAndProperty = key.substring(prefix.length()); int sepIndx = nameAndProperty.indexOf(SEPARATOR); if (sepIndx != -1) { String beanName = nameAndProperty.substring(0, sepIndx); logger.debug("Found bean name '" + beanName + "'"); if (!getBeanFactory().containsBeanDefinition(beanName)) { // If we haven't already registered it... registerBeanDefinition(beanName, m, prefix + beanName, resourceDescription); ++beanCount; } } else { // Ignore it: it wasn't a valid bean name and property, // although it did start with the required prefix logger.debug("Invalid bean name and property [" + nameAndProperty + "]"); } } // if the key started with the prefix we're looking for } // while there are more keys return beanCount; }
/** * Register bean definitions contained in a Map. Ignore ineligible properties. * * @param map Map name -> property (String or Object). Property values will be strings if coming * from a Properties file etc. Property names (keys) <b>must</b> be strings. Class keys must * be Strings. * @param prefix match or filter within the keys in the map: e.g. 'beans.' (can be empty or <code> * null</code>) * @param resourceDescription description of the resource that the Map came from (for logging * purposes) * @return the number of bean definitions found * @throws BeansException in case of loading or parsing errors * @see #registerBeanDefinitions(Map, String) */ public int registerBeanDefinitions(Map map, String prefix, String resourceDescription) throws BeansException { if (prefix == null) { prefix = ""; } int beanCount = 0; for (Iterator it = map.keySet().iterator(); it.hasNext(); ) { Object key = it.next(); if (!(key instanceof String)) { throw new IllegalArgumentException("Illegal key [" + key + "]: only Strings allowed"); } String keyString = (String) key; if (keyString.startsWith(prefix)) { // Key is of form: prefix<name>.property String nameAndProperty = keyString.substring(prefix.length()); // Find dot before property name, ignoring dots in property keys. int sepIdx = -1; int propKeyIdx = nameAndProperty.indexOf(PropertyAccessor.PROPERTY_KEY_PREFIX); if (propKeyIdx != -1) { sepIdx = nameAndProperty.lastIndexOf(SEPARATOR, propKeyIdx); } else { sepIdx = nameAndProperty.lastIndexOf(SEPARATOR); } if (sepIdx != -1) { String beanName = nameAndProperty.substring(0, sepIdx); if (logger.isDebugEnabled()) { logger.debug("Found bean name '" + beanName + "'"); } if (!getBeanFactory().containsBeanDefinition(beanName)) { // If we haven't already registered it... registerBeanDefinition(beanName, map, prefix + beanName, resourceDescription); ++beanCount; } } else { // Ignore it: It wasn't a valid bean name and property, // although it did start with the required prefix. if (logger.isDebugEnabled()) { logger.debug("Invalid bean name and property [" + nameAndProperty + "]"); } } } } return beanCount; }