1 public class DataSource { 2 3 /** 4 * 驱动类 5 */ 6 private String driveClass; 7 8 /** 9 * jdbc地址 10 */ 11 private String url; 12 13 /** 14 * 用户名 15 */ 16 private String userName; 17 18 /** 19 * 密码 20 */ 21 private String password; 22 23 public String getDriveClass() { 24 return driveClass; 25 } 26 27 public void setDriveClass(String driveClass) { 28 this.driveClass = driveClass; 29 } 30 31 public String getUrl() { 32 return url; 33 } 34 35 public void setUrl(String url) { 36 this.url = url; 37 } 38 39 public String getUserName() { 40 return userName; 41 } 42 43 public void setUserName(String userName) { 44 this.userName = userName; 45 } 46 47 public String getPassword() { 48 return password; 49 } 50 51 public void setPassword(String password) { 52 this.password = password; 53 } 54 55 @Override 56 public String toString() { 57 return "DataSource [driveClass=" + driveClass + ", url=" + url + ", userName=" + userName + ", password=" + password + "]"; 58 } 59 60 }
1 driveClass=0 2 url=1 3 userName=2 4 password=3
1 23 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:aop="http://www.springframework.org/schema/aop" 5 xmlns:tx="http://www.springframework.org/schema/tx" 6 xsi:schemaLocation="http://www.springframework.org/schema/beans 7 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 8 http://www.springframework.org/schema/aop 9 http://www.springframework.org/schema/aop/spring-aop-3.0.xsd"> 10 11 class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 12 14 1513 class="org.xrq.spring.action.properties.DataSource"> 16 21 2217 18 19 20
1 public class TestProperties { 2 3 @Test 4 public void testProperties() { 5 ApplicationContext ac = new ClassPathXmlApplicationContext("spring/properties.xml"); 6 7 DataSource dataSource = (DataSource)ac.getBean("dataSource"); 8 System.out.println(dataSource); 9 } 10 11 }
1 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 2 try { 3 Properties mergedProps = mergeProperties(); 4 5 // Convert the merged properties, if necessary. 6 convertProperties(mergedProps); 7 8 // Let the subclass process the properties. 9 processProperties(beanFactory, mergedProps); 10 } 11 catch (IOException ex) { 12 throw new BeanInitializationException("Could not load properties", ex); 13 } 14 }
1 protected Properties mergeProperties() throws IOException { 2 Properties result = new Properties(); 3 4 if (this.localOverride) { 5 // Load properties from file upfront, to let local properties override. 6 loadProperties(result); 7 } 8 9 if (this.localProperties != null) { 10 for (Properties localProp : this.localProperties) { 11 CollectionUtils.mergePropertiesIntoMap(localProp, result); 12 } 13 } 14 15 if (!this.localOverride) { 16 // Load properties from file afterwards, to let those properties override. 17 loadProperties(result); 18 } 19 20 return result; 21 }
1 protected void loadProperties(Properties props) throws IOException { 2 if (this.locations != null) { 3 for (Resource location : this.locations) { 4 if (logger.isInfoEnabled()) { 5 logger.info("Loading properties file from " + location); 6 } 7 InputStream is = null; 8 try { 9 is = location.getInputStream(); 10 11 String filename = null; 12 try { 13 filename = location.getFilename(); 14 } catch (IllegalStateException ex) { 15 // resource is not file-based. See SPR-7552. 16 } 17 18 if (filename != null && filename.endsWith(XML_FILE_EXTENSION)) { 19 this.propertiesPersister.loadFromXml(props, is); 20 } 21 else { 22 if (this.fileEncoding != null) { 23 this.propertiesPersister.load(props, new InputStreamReader(is, this.fileEncoding)); 24 } 25 else { 26 this.propertiesPersister.load(props, is); 27 } 28 } 29 } 30 catch (IOException ex) { 31 if (this.ignoreResourceNotFound) { 32 if (logger.isWarnEnabled()) { 33 logger.warn("Could not load properties from " + location + ": " + ex.getMessage()); 34 } 35 } 36 else { 37 throw ex; 38 } 39 } 40 finally { 41 if (is != null) { 42 is.close(); 43 } 44 } 45 } 46 } 47 }
1 public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { 2 try { 3 Properties mergedProps = mergeProperties(); 4 5 // Convert the merged properties, if necessary. 6 convertProperties(mergedProps); 7 8 // Let the subclass process the properties. 9 processProperties(beanFactory, mergedProps); 10 } 11 catch (IOException ex) { 12 throw new BeanInitializationException("Could not load properties", ex); 13 } 14 }
1 protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props) 2 throws BeansException { 3 4 StringValueResolver valueResolver = new PlaceholderResolvingStringValueResolver(props); 5 BeanDefinitionVisitor visitor = new BeanDefinitionVisitor(valueResolver); 6 7 String[] beanNames = beanFactoryToProcess.getBeanDefinitionNames(); 8 for (String curName : beanNames) { 9 // Check that we're not parsing our own bean definition, 10 // to avoid failing on unresolvable placeholders in properties file locations. 11 if (!(curName.equals(this.beanName) && beanFactoryToProcess.equals(this.beanFactory))) { 12 BeanDefinition bd = beanFactoryToProcess.getBeanDefinition(curName); 13 try { 14 visitor.visitBeanDefinition(bd); 15 } 16 catch (Exception ex) { 17 throw new BeanDefinitionStoreException(bd.getResourceDescription(), curName, ex.getMessage()); 18 } 19 } 20 } 21 22 // New in Spring 2.5: resolve placeholders in alias target names and aliases as well. 23 beanFactoryToProcess.resolveAliases(valueResolver); 24 25 // New in Spring 3.0: resolve placeholders in embedded values such as annotation attributes. 26 beanFactoryToProcess.addEmbeddedValueResolver(valueResolver); 27 }
1 public void visitBeanDefinition(BeanDefinition beanDefinition) { 2 visitParentName(beanDefinition); 3 visitBeanClassName(beanDefinition); 4 visitFactoryBeanName(beanDefinition); 5 visitFactoryMethodName(beanDefinition); 6 visitScope(beanDefinition); 7 visitPropertyValues(beanDefinition.getPropertyValues()); 8 ConstructorArgumentValues cas = beanDefinition.getConstructorArgumentValues(); 9 visitIndexedArgumentValues(cas.getIndexedArgumentValues()); 10 visitGenericArgumentValues(cas.getGenericArgumentValues()); 11 }
1 protected void visitPropertyValues(MutablePropertyValues pvs) { 2 PropertyValue[] pvArray = pvs.getPropertyValues(); 3 for (PropertyValue pv : pvArray) { 4 Object newVal = resolveValue(pv.getValue()); 5 if (!ObjectUtils.nullSafeEquals(newVal, pv.getValue())) { 6 pvs.add(pv.getName(), newVal); 7 } 8 } 9 }
1 protected Object resolveValue(Object value) { 2 if (value instanceof BeanDefinition) { 3 visitBeanDefinition((BeanDefinition) value); 4 } 5 else if (value instanceof BeanDefinitionHolder) { 6 visitBeanDefinition(((BeanDefinitionHolder) value).getBeanDefinition()); 7 } 8 else if (value instanceof RuntimeBeanReference) { 9 RuntimeBeanReference ref = (RuntimeBeanReference) value; 10 String newBeanName = resolveStringValue(ref.getBeanName()); 11 if (!newBeanName.equals(ref.getBeanName())) { 12 return new RuntimeBeanReference(newBeanName); 13 } 14 } 15 else if (value instanceof RuntimeBeanNameReference) { 16 RuntimeBeanNameReference ref = (RuntimeBeanNameReference) value; 17 String newBeanName = resolveStringValue(ref.getBeanName()); 18 if (!newBeanName.equals(ref.getBeanName())) { 19 return new RuntimeBeanNameReference(newBeanName); 20 } 21 } 22 else if (value instanceof Object[]) { 23 visitArray((Object[]) value); 24 } 25 else if (value instanceof List) { 26 visitList((List) value); 27 } 28 else if (value instanceof Set) { 29 visitSet((Set) value); 30 } 31 else if (value instanceof Map) { 32 visitMap((Map) value); 33 } 34 else if (value instanceof TypedStringValue) { 35 TypedStringValue typedStringValue = (TypedStringValue) value; 36 String stringValue = typedStringValue.getValue(); 37 if (stringValue != null) { 38 String visitedString = resolveStringValue(stringValue); 39 typedStringValue.setValue(visitedString); 40 } 41 } 42 else if (value instanceof String) { 43 return resolveStringValue((String) value); 44 } 45 return value; 46 }
1 protected String resolveStringValue(String strVal) { 2 if (this.valueResolver == null) { 3 throw new IllegalStateException("No StringValueResolver specified - pass a resolver " + 4 "object into the constructor or override the 'resolveStringValue' method"); 5 } 6 String resolvedValue = this.valueResolver.resolveStringValue(strVal); 7 // Return original String if not modified. 8 return (strVal.equals(resolvedValue) ? strVal : resolvedValue); 9 }
1 public String resolveStringValue(String strVal) throws BeansException { 2 String value = this.helper.replacePlaceholders(strVal, this.resolver); 3 return (value.equals(nullValue) ? null : value); 4 }
1 public String replacePlaceholders(String value, PlaceholderResolver placeholderResolver) { 2 Assert.notNull(value, "Argument 'value' must not be null."); 3 return parseStringValue(value, placeholderResolver, new HashSet()); 4 }
1 protected String parseStringValue( 2 String strVal, PlaceholderResolver placeholderResolver, SetvisitedPlaceholders) { 3 4 StringBuilder buf = new StringBuilder(strVal); 5 6 int startIndex = strVal.indexOf(this.placeholderPrefix); 7 while (startIndex != -1) { 8 int endIndex = findPlaceholderEndIndex(buf, startIndex); 9 if (endIndex != -1) { 10 String placeholder = buf.substring(startIndex + this.placeholderPrefix.length(), endIndex); 11 if (!visitedPlaceholders.add(placeholder)) { 12 throw new IllegalArgumentException( 13 "Circular placeholder reference '" + placeholder + "' in property definitions"); 14 } 15 // Recursive invocation, parsing placeholders contained in the placeholder key. 16 placeholder = parseStringValue(placeholder, placeholderResolver, visitedPlaceholders); 17 18 // Now obtain the value for the fully resolved key... 19 String propVal = placeholderResolver.resolvePlaceholder(placeholder); 20 if (propVal == null && this.valueSeparator != null) { 21 int separatorIndex = placeholder.indexOf(this.valueSeparator); 22 if (separatorIndex != -1) { 23 String actualPlaceholder = placeholder.substring(0, separatorIndex); 24 String defaultValue = placeholder.substring(separatorIndex + this.valueSeparator.length()); 25 propVal = placeholderResolver.resolvePlaceholder(actualPlaceholder); 26 if (propVal == null) { 27 propVal = defaultValue; 28 } 29 } 30 } 31 if (propVal != null) { 32 // Recursive invocation, parsing placeholders contained in the 33 // previously resolved placeholder value. 34 propVal = parseStringValue(propVal, placeholderResolver, visitedPlaceholders); 35 buf.replace(startIndex, endIndex + this.placeholderSuffix.length(), propVal); 36 if (logger.isTraceEnabled()) { 37 logger.trace("Resolved placeholder '" + placeholder + "'"); 38 } 39 startIndex = buf.indexOf(this.placeholderPrefix, startIndex + propVal.length()); 40 } 41 else if (this.ignoreUnresolvablePlaceholders) { 42 // Proceed with unprocessed value. 43 startIndex = buf.indexOf(this.placeholderPrefix, endIndex + this.placeholderSuffix.length()); 44 } 45 else { 46 throw new IllegalArgumentException("Could not resolve placeholder '" + placeholder + "'"); 47 } 48 49 visitedPlaceholders.remove(placeholder); 50 } 51 else { 52 startIndex = -1; 53 } 54 } 55 56 return buf.toString(); 57 }
流程很长,通过这样一整个的流程,将占位符"${...}"中的内容替换为了我们需要的值。
转载地址:http://www.3fwork.com/b200/009295MYM023659/