1 16 17 package org.springframework.beans.factory.config; 18 19 import java.util.Collections ; 20 import java.util.Enumeration ; 21 import java.util.HashSet ; 22 import java.util.Properties ; 23 import java.util.Set ; 24 25 import org.springframework.beans.BeansException; 26 import org.springframework.beans.factory.BeanInitializationException; 27 28 61 public class PropertyOverrideConfigurer extends PropertyResourceConfigurer { 62 63 public static final String DEFAULT_BEAN_NAME_SEPARATOR = "."; 64 65 66 private String beanNameSeparator = DEFAULT_BEAN_NAME_SEPARATOR; 67 68 private boolean ignoreInvalidKeys = false; 69 70 71 private Set beanNames = Collections.synchronizedSet(new HashSet ()); 72 73 74 78 public void setBeanNameSeparator(String beanNameSeparator) { 79 this.beanNameSeparator = beanNameSeparator; 80 } 81 82 88 public void setIgnoreInvalidKeys(boolean ignoreInvalidKeys) { 89 this.ignoreInvalidKeys = ignoreInvalidKeys; 90 } 91 92 93 protected void processProperties(ConfigurableListableBeanFactory beanFactory, Properties props) 94 throws BeansException { 95 96 for (Enumeration names = props.propertyNames(); names.hasMoreElements();) { 97 String key = (String ) names.nextElement(); 98 try { 99 processKey(beanFactory, key, props.getProperty(key)); 100 } 101 catch (BeansException ex) { 102 String msg = "Could not process key '" + key + "' in PropertyOverrideConfigurer"; 103 if (!this.ignoreInvalidKeys) { 104 throw new BeanInitializationException(msg, ex); 105 } 106 if (logger.isDebugEnabled()) { 107 logger.debug(msg, ex); 108 } 109 } 110 } 111 } 112 113 116 protected void processKey(ConfigurableListableBeanFactory factory, String key, String value) 117 throws BeansException { 118 119 int separatorIndex = key.indexOf(this.beanNameSeparator); 120 if (separatorIndex == -1) { 121 throw new BeanInitializationException("Invalid key '" + key + 122 "': expected 'beanName" + this.beanNameSeparator + "property'"); 123 } 124 String beanName = key.substring(0, separatorIndex); 125 String beanProperty = key.substring(separatorIndex+1); 126 this.beanNames.add(beanName); 127 applyPropertyValue(factory, beanName, beanProperty, value); 128 if (logger.isDebugEnabled()) { 129 logger.debug("Property '" + key + "' set to value [" + value + "]"); 130 } 131 } 132 133 136 protected void applyPropertyValue( 137 ConfigurableListableBeanFactory factory, String beanName, String property, String value) { 138 139 BeanDefinition bd = factory.getBeanDefinition(beanName); 140 bd.getPropertyValues().addPropertyValue(property, value); 141 } 142 143 144 151 public boolean hasPropertyOverridesFor(String beanName) { 152 return this.beanNames.contains(beanName); 153 } 154 155 } 156 | Popular Tags |