/// <summary> /// 当配置更新时,被调用 /// </summary> /// <param name="event"> </param> public virtual void propertiesReloaded(PropertiesReloadedEvent @event) { Properties oldProperties = lastMergedProperties; try { // Properties newProperties = mergeProperties(); // // 获取哪些 dynamic property 被影响 // ISet<string> placeholders = placeholderToDynamics.Keys; ISet<DynamicProperty> allDynamics = new HashSet<DynamicProperty>(); foreach (string placeholder in placeholders) { string newValue = newProperties.getProperty(placeholder); string oldValue = oldProperties.getProperty(placeholder); if (!string.ReferenceEquals(newValue, null) && !newValue.Equals(oldValue) || string.ReferenceEquals(newValue, null) && !string.ReferenceEquals(oldValue, null)) { if (logger.InfoEnabled) { logger.info("Property changed detected: " + placeholder + (!string.ReferenceEquals(newValue, null) ? "=" + newValue : " removed")); } IList<DynamicProperty> affectedDynamics = placeholderToDynamics[placeholder]; allDynamics.addAll(affectedDynamics); } } // // 获取受影响的beans // IDictionary<string, IList<DynamicProperty>> dynamicsByBeanName = new Dictionary<string, IList<DynamicProperty>>(); IDictionary<string, object> beanByBeanName = new Dictionary<string, object>(); foreach (DynamicProperty dynamic in allDynamics) { string beanName = dynamic.BeanName; IList<DynamicProperty> l = dynamicsByBeanName[beanName]; if (l == null) { dynamicsByBeanName[beanName] = (l = new List<DynamicProperty>()); object bean = null; try { bean = applicationContext.getBean(beanName); beanByBeanName[beanName] = bean; } catch (BeansException e) { // keep dynamicsByBeanName list, warn only once. logger.error("Error obtaining bean " + beanName, e); } // // say hello // try { if (bean is IReconfigurationAware) { ((IReconfigurationAware) bean).beforeReconfiguration(); // hello! } } catch (Exception e) { logger.error("Error calling beforeReconfiguration on " + beanName, e); } } l.Add(dynamic); } // // 处理受影响的bean // IDictionary<string, IList<DynamicProperty>>.KeyCollection beanNames = dynamicsByBeanName.Keys; foreach (string beanName in beanNames) { object bean = beanByBeanName[beanName]; if (bean == null) // problems obtaining bean, earlier { continue; } BeanWrapper beanWrapper = new BeanWrapperImpl(bean); // for all affected ... IList<DynamicProperty> dynamics = dynamicsByBeanName[beanName]; foreach (DynamicProperty dynamic in dynamics) { string propertyName = dynamic.PropertyName; string unparsedValue = dynamic.UnparsedValue; // obtain an updated value, including dependencies string newValue; removeDynamic(dynamic); currentBeanName = beanName; currentPropertyName = propertyName; try { newValue = parseStringValue(unparsedValue, newProperties, new HashSet()); } finally { currentBeanName = null; currentPropertyName = null; } if (logger.InfoEnabled) { logger.info("Updating property " + beanName + "." + propertyName + " to " + newValue); } // assign it to the bean try { beanWrapper.setPropertyValue(propertyName, newValue); } catch (BeansException e) { logger.error("Error setting property " + beanName + "." + propertyName + " to " + newValue, e); } } } // // say goodbye. // foreach (string beanName in beanNames) { object bean = beanByBeanName[beanName]; try { if (bean is IReconfigurationAware) { ((IReconfigurationAware) bean).afterReconfiguration(); } } catch (Exception e) { logger.error("Error calling afterReconfiguration on " + beanName, e); } } } catch (IOException e) { logger.error("Error trying to reload net.unicon.iamlabs.spring.properties.example.net.unicon.iamlabs" + ".spring" + ".properties: " + e.Message, e); } }
/// <summary> /// 通过listener去通知 reload /// </summary> /// <param name="oldProperties"> </param> protected internal virtual void notifyPropertiesChanged(Properties oldProperties) { PropertiesReloadedEvent @event = new PropertiesReloadedEvent(this, oldProperties); foreach (IReloadablePropertiesListener listener in listeners) { listener.propertiesReloaded(@event); } }