Spring PropertyPlaceholderConfigurerResolver

  • 类全路径: org.springframework.beans.factory.config.PropertyPlaceholderConfigurer.PropertyPlaceholderConfigurerResolver

  • 这个类是从 Properties 中获取属性

  1. private final class PropertyPlaceholderConfigurerResolver implements PlaceholderResolver {
  2. private final Properties props;
  3. private PropertyPlaceholderConfigurerResolver(Properties props) {
  4. this.props = props;
  5. }
  6. @Override
  7. @Nullable
  8. public String resolvePlaceholder(String placeholderName) {
  9. return PropertyPlaceholderConfigurer.this.resolvePlaceholder(placeholderName,
  10. this.props, systemPropertiesMode);
  11. }
  12. }
  • 详细方法如下
  1. @Nullable
  2. protected String resolvePlaceholder(String placeholder, Properties props, int systemPropertiesMode) {
  3. String propVal = null;
  4. if (systemPropertiesMode == SYSTEM_PROPERTIES_MODE_OVERRIDE) {
  5. propVal = resolveSystemProperty(placeholder);
  6. }
  7. if (propVal == null) {
  8. propVal = resolvePlaceholder(placeholder, props);
  9. }
  10. if (propVal == null && systemPropertiesMode == SYSTEM_PROPERTIES_MODE_FALLBACK) {
  11. propVal = resolveSystemProperty(placeholder);
  12. }
  13. return propVal;
  14. }
  1. @Nullable
  2. protected String resolvePlaceholder(String placeholder, Properties props) {
  3. return props.getProperty(placeholder);
  4. }
  1. @Nullable
  2. protected String resolveSystemProperty(String key) {
  3. try {
  4. String value = System.getProperty(key);
  5. if (value == null && this.searchSystemEnvironment) {
  6. value = System.getenv(key);
  7. }
  8. return value;
  9. }
  10. catch (Throwable ex) {
  11. if (logger.isDebugEnabled()) {
  12. logger.debug("Could not access system property '" + key + "': " + ex);
  13. }
  14. return null;
  15. }
  16. }