1 16 17 package org.springframework.web.context.support; 18 19 import java.util.Properties ; 20 21 import javax.servlet.ServletContext ; 22 23 import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer; 24 import org.springframework.web.context.ServletContextAware; 25 26 61 public class ServletContextPropertyPlaceholderConfigurer extends PropertyPlaceholderConfigurer 62 implements ServletContextAware { 63 64 private boolean contextOverride = false; 65 66 private boolean searchContextAttributes = false; 67 68 private ServletContext servletContext; 69 70 71 81 public void setContextOverride(boolean contextOverride) { 82 this.contextOverride = contextOverride; 83 } 84 85 97 public void setSearchContextAttributes(boolean searchContextAttributes) { 98 this.searchContextAttributes = searchContextAttributes; 99 } 100 101 108 public void setServletContext(ServletContext servletContext) { 109 this.servletContext = servletContext; 110 } 111 112 113 protected String resolvePlaceholder(String placeholder, Properties props) { 114 String value = null; 115 if (this.contextOverride && this.servletContext != null) { 116 value = resolvePlaceholder(placeholder, this.servletContext, this.searchContextAttributes); 117 } 118 if (value == null) { 119 value = super.resolvePlaceholder(placeholder, props); 120 } 121 if (value == null && this.servletContext != null) { 122 value = resolvePlaceholder(placeholder, this.servletContext, this.searchContextAttributes); 123 } 124 return value; 125 } 126 127 142 protected String resolvePlaceholder( 143 String placeholder, ServletContext servletContext, boolean searchContextAttributes) { 144 145 String value = null; 146 if (searchContextAttributes) { 147 Object attrValue = servletContext.getAttribute(placeholder); 148 if (attrValue != null) { 149 value = attrValue.toString(); 150 } 151 } 152 if (value == null) { 153 value = servletContext.getInitParameter(placeholder); 154 } 155 return value; 156 } 157 158 } 159 | Popular Tags |