org.springframework.core.env.MapPropertySource类的使用及代码示例

x33g5p2x  于2022-01-24 转载在 其他  
字(14.5k)|赞(0)|评价(0)|浏览(203)

本文整理了Java中org.springframework.core.env.MapPropertySource类的一些代码示例,展示了MapPropertySource类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。MapPropertySource类的具体详情如下:
包路径:org.springframework.core.env.MapPropertySource
类名称:MapPropertySource

MapPropertySource介绍

[英]PropertySource that reads keys and values from a Map object.
[中]从映射对象读取键和值的PropertySource。

代码示例

代码示例来源:origin: spring-cloud/spring-cloud-gateway

@Override
  public void postProcessEnvironment(ConfigurableEnvironment env, SpringApplication application) {
    env.getPropertySources().addFirst(new MapPropertySource("gateway-properties",
        Collections.singletonMap("spring.webflux.hiddenmethod.filter.enabled", "false")));
  }
}

代码示例来源:origin: spring-cloud-incubator/spring-cloud-alibaba

/**
 * Copy from {@link BusEnvironmentPostProcessor#addOrReplace(MutablePropertySources, Map)}
 *
 * @param propertySources {@link MutablePropertySources}
 * @param map             Default RocketMQ Bus Properties
 */
private void addOrReplace(MutablePropertySources propertySources,
             Map<String, Object> map) {
  MapPropertySource target = null;
  if (propertySources.contains(PROPERTY_SOURCE_NAME)) {
    PropertySource<?> source = propertySources.get(PROPERTY_SOURCE_NAME);
    if (source instanceof MapPropertySource) {
      target = (MapPropertySource) source;
      for (String key : map.keySet()) {
        if (!target.containsProperty(key)) {
          target.getSource().put(key, map.get(key));
        }
      }
    }
  }
  if (target == null) {
    target = new MapPropertySource(PROPERTY_SOURCE_NAME, map);
  }
  if (!propertySources.contains(PROPERTY_SOURCE_NAME)) {
    propertySources.addLast(target);
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * This implementation returns {@code true} if a property with the given name or
 * any underscore/uppercase variant thereof exists in this property source.
 */
@Override
@Nullable
public Object getProperty(String name) {
  String actualName = resolvePropertyName(name);
  if (logger.isDebugEnabled() && !name.equals(actualName)) {
    logger.debug("PropertySource '" + getName() + "' does not contain property '" + name +
        "', but found equivalent '" + actualName + "'");
  }
  return super.getProperty(actualName);
}

代码示例来源:origin: spring-projects/spring-framework

environment.getPropertySources().get(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME);
if (ps == null) {
  ps = new MapPropertySource(INLINED_PROPERTIES_PROPERTY_SOURCE_NAME, new LinkedHashMap<>());
  environment.getPropertySources().addFirst(ps);
ps.getSource().putAll(convertInlinedPropertiesToMap(inlinedProperties));

代码示例来源:origin: spring-projects/spring-framework

@Test
@SuppressWarnings("serial")
public void equals() {
  Map<String, Object> map1 = new HashMap<String, Object>() {{ put("a", "b"); }};
  Map<String, Object> map2 = new HashMap<String, Object>() {{ put("c", "d"); }};
  Properties props1 = new Properties() {{ setProperty("a", "b"); }};
  Properties props2 = new Properties() {{ setProperty("c", "d"); }};
  MapPropertySource mps = new MapPropertySource("mps", map1);
  assertThat(mps, equalTo(mps));
  assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("x", map1)), is(true));
  assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("x", map2)), is(true));
  assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("x", props1)), is(true));
  assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("x", props2)), is(true));
  assertThat(new MapPropertySource("x", map1).equals(new Object()), is(false));
  assertThat(new MapPropertySource("x", map1).equals("x"), is(false));
  assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("y", map1)), is(false));
  assertThat(new MapPropertySource("x", map1).equals(new MapPropertySource("y", map2)), is(false));
  assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("y", props1)), is(false));
  assertThat(new MapPropertySource("x", map1).equals(new PropertiesPropertySource("y", props2)), is(false));
}

代码示例来源:origin: spring-projects/spring-integration-samples

@Override
protected void loadBeanDefinitions(GenericApplicationContext context,
    MergedContextConfiguration mergedConfig) {
  int availableServerSocket = SocketUtils.findAvailableTcpPort(5678);
  final Map<String, Object> sockets = new HashMap<String, Object>();
  sockets.put("availableServerSocket", availableServerSocket);
  if (LOGGER.isInfoEnabled()) {
    LOGGER.info("Available Server Socket: " + availableServerSocket);
  }
  final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);
  context.getEnvironment().getPropertySources().addLast(propertySource);
  super.loadBeanDefinitions(context, mergedConfig);
}

代码示例来源:origin: spring-projects/spring-integration-samples

public static GenericXmlApplicationContext setupContext() {
  final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
  if (System.getProperty(AVAILABLE_SERVER_SOCKET) == null) {
    System.out.print("Detect open server socket...");
    int availableServerSocket = SocketUtils.findAvailableTcpPort(5678);
    final Map<String, Object> sockets = new HashMap<>();
    sockets.put(AVAILABLE_SERVER_SOCKET, availableServerSocket);
    final MapPropertySource propertySource = new MapPropertySource("sockets", sockets);
    context.getEnvironment().getPropertySources().addLast(propertySource);
  }
  System.out.println("using port " + context.getEnvironment().getProperty(AVAILABLE_SERVER_SOCKET));
  context.load("classpath:META-INF/spring/integration/tcpClientServerDemo-context.xml");
  context.registerShutdownHook();
  context.refresh();
  return context;
}

代码示例来源:origin: org.springframework.boot/spring-boot

/**
 * Add, remove or re-order any {@link PropertySource}s in this application's
 * environment.
 * @param environment this application's environment
 * @param args arguments passed to the {@code run} method
 * @see #configureEnvironment(ConfigurableEnvironment, String[])
 */
protected void configurePropertySources(ConfigurableEnvironment environment,
    String[] args) {
  MutablePropertySources sources = environment.getPropertySources();
  if (this.defaultProperties != null && !this.defaultProperties.isEmpty()) {
    sources.addLast(
        new MapPropertySource("defaultProperties", this.defaultProperties));
  }
  if (this.addCommandLineProperties && args.length > 0) {
    String name = CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME;
    if (sources.contains(name)) {
      PropertySource<?> source = sources.get(name);
      CompositePropertySource composite = new CompositePropertySource(name);
      composite.addPropertySource(new SimpleCommandLinePropertySource(
          "springApplicationCommandLineArgs", args));
      composite.addPropertySource(source);
      sources.replace(name, composite);
    }
    else {
      sources.addFirst(new SimpleCommandLinePropertySource(args));
    }
  }
}

代码示例来源:origin: org.springframework.cloud/spring-cloud-context

@ManagedOperation
public void setProperty(String name, String value) {
  if (!environment.getPropertySources().contains(MANAGER_PROPERTY_SOURCE)) {
    synchronized (map) {
      if (!environment.getPropertySources().contains(MANAGER_PROPERTY_SOURCE)) {
        MapPropertySource source = new MapPropertySource(
            MANAGER_PROPERTY_SOURCE, map);
        environment.getPropertySources().addFirst(source);
      }
    }
  }
  if (!value.equals(environment.getProperty(name))) {
    map.put(name, value);
    publish(new EnvironmentChangeEvent(publisher, Collections.singleton(name)));
  }
}

代码示例来源:origin: org.springframework.metrics/spring-metrics

private void addDefaultProperty(ConfigurableEnvironment environment, String name,
                  String value) {
    MutablePropertySources sources = environment.getPropertySources();
    Map<String, Object> map = null;
    if (sources.contains("defaultProperties")) {
      PropertySource<?> source = sources.get("defaultProperties");
      if (source instanceof MapPropertySource) {
        map = ((MapPropertySource) source).getSource();
      }
    } else {
      map = new LinkedHashMap<>();
      sources.addLast(new MapPropertySource("defaultProperties", map));
    }
    if (map != null) {
      map.put(name, value);
    }
  }
}

代码示例来源:origin: org.springframework.cloud/spring-cloud-context

private StandardEnvironment copyEnvironment(ConfigurableEnvironment input) {
  StandardEnvironment environment = new StandardEnvironment();
  MutablePropertySources capturedPropertySources = environment.getPropertySources();
  // Only copy the default property source(s) and the profiles over from the main
  // environment (everything else should be pristine, just like it was on startup).
  for (String name : DEFAULT_PROPERTY_SOURCES) {
    if (input.getPropertySources().contains(name)) {
      if (capturedPropertySources.contains(name)) {
        capturedPropertySources.replace(name,
            input.getPropertySources().get(name));
      }
      else {
        capturedPropertySources.addLast(input.getPropertySources().get(name));
      }
    }
  }
  environment.setActiveProfiles(input.getActiveProfiles());
  environment.setDefaultProfiles(input.getDefaultProfiles());
  Map<String, Object> map = new HashMap<String, Object>();
  map.put("spring.jmx.enabled", false);
  map.put("spring.main.sources", "");
  capturedPropertySources
      .addFirst(new MapPropertySource(REFRESH_ARGS_PROPERTY_SOURCE, map));
  return environment;
}

代码示例来源:origin: org.springframework.cloud/spring-cloud-context

.getPropertySources();
for (PropertySource<?> source : bootstrapProperties) {
  bootstrapProperties.remove(source.getName());
    .resolvePlaceholders("${spring.cloud.bootstrap.location:}");
Map<String, Object> bootstrapMap = new HashMap<>();
bootstrapMap.put("spring.config.name", configName);
  bootstrapMap.put("spring.config.location", configLocation);
bootstrapProperties.addFirst(
    new MapPropertySource(BOOTSTRAP_PROPERTY_SOURCE_NAME, bootstrapMap));
for (PropertySource<?> source : environment.getPropertySources()) {
  if (source instanceof StubPropertySource) {
    continue;
  bootstrapProperties.addLast(source);
    .profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF)
    .environment(bootstrapEnvironment)

代码示例来源:origin: apache/servicecomb-java-chassis

if (configCenterConfigurationSource != null) {
 try {
  ce.getPropertySources()
    .addFirst(new MapPropertySource("dynamic-source", configCenterConfigurationSource.getCurrentData()));
 } catch (Exception e) {
  LOGGER.warn("set up spring property source failed. msg: {}", e.getMessage());
ce.getPropertySources().addLast(
  new EnumerablePropertySource<ConcurrentCompositeConfiguration>("microservice.yaml",
    concurrentCompositeConfiguration) {

代码示例来源:origin: org.springframework.cloud/spring-cloud-context

private void reorderSources(ConfigurableEnvironment environment) {
  PropertySource<?> removed = environment.getPropertySources()
      .remove(DEFAULT_PROPERTIES);
  if (removed instanceof ExtendedDefaultPropertySource) {
    ExtendedDefaultPropertySource defaultProperties = (ExtendedDefaultPropertySource) removed;
    environment.getPropertySources().addLast(new MapPropertySource(
        DEFAULT_PROPERTIES, defaultProperties.getSource()));
    for (PropertySource<?> source : defaultProperties.getPropertySources()
        .getPropertySources()) {
      if (!environment.getPropertySources().contains(source.getName())) {
        environment.getPropertySources().addBefore(DEFAULT_PROPERTIES,
            source);
      }
    }
  }
}

代码示例来源:origin: spring-projects/spring-framework

/**
 * Customize the set of property sources with those appropriate for any standard
 * Java environment:
 * <ul>
 * <li>{@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME}
 * <li>{@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}
 * </ul>
 * <p>Properties present in {@value #SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME} will
 * take precedence over those in {@value #SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME}.
 * @see AbstractEnvironment#customizePropertySources(MutablePropertySources)
 * @see #getSystemProperties()
 * @see #getSystemEnvironment()
 */
@Override
protected void customizePropertySources(MutablePropertySources propertySources) {
  propertySources.addLast(new MapPropertySource(SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME, getSystemProperties()));
  propertySources.addLast(new SystemEnvironmentPropertySource(SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, getSystemEnvironment()));
}

代码示例来源:origin: org.springframework.boot/spring-boot-devtools

@Override
public void postProcessEnvironment(ConfigurableEnvironment environment,
    SpringApplication application) {
  if (isLocalApplication(environment)) {
    if (canAddProperties(environment)) {
      logger.info("Devtools property defaults active! Set '" + ENABLED
          + "' to 'false' to disable");
      environment.getPropertySources()
          .addLast(new MapPropertySource("devtools", PROPERTIES));
    }
    if (isWebApplication(environment)
        && !environment.containsProperty(WEB_LOGGING)) {
      logger.info("For additional web related logging consider "
          + "setting the '" + WEB_LOGGING + "' property to 'DEBUG'");
    }
  }
}

代码示例来源:origin: org.springframework.boot/spring-boot

private PropertyResolver getTitleResolver(Class<?> sourceClass) {
  MutablePropertySources sources = new MutablePropertySources();
  String applicationTitle = getApplicationTitle(sourceClass);
  Map<String, Object> titleMap = Collections.singletonMap("application.title",
      (applicationTitle != null) ? applicationTitle : "");
  sources.addFirst(new MapPropertySource("title", titleMap));
  return new PropertySourcesPropertyResolver(sources);
}

代码示例来源:origin: com.alipay.sofa/infra-sofa-boot-starter

/**
 * config log settings
 */
private void assemblyLogSetting(ConfigurableEnvironment environment) {
  StreamSupport.stream(environment.getPropertySources().spliterator(), false)
    .filter(propertySource -> propertySource instanceof EnumerablePropertySource)
    .map(propertySource -> Arrays
      .asList(((MapPropertySource) propertySource).getPropertyNames()))
      .flatMap(Collection::stream).filter(LogEnvUtils::filterAllLogConfig)
      .forEach((key) -> HIGH_PRIORITY_CONFIG.getSource().put(key, environment.getProperty(key)));
}

代码示例来源:origin: org.springframework.boot/spring-boot

private PropertyResolver getVersionResolver(Class<?> sourceClass) {
  MutablePropertySources propertySources = new MutablePropertySources();
  propertySources
      .addLast(new MapPropertySource("version", getVersionsMap(sourceClass)));
  return new PropertySourcesPropertyResolver(propertySources);
}

代码示例来源:origin: rayokota/kafka-graphs

private void logActiveProperties(ConfigurableEnvironment env) {
    List<MapPropertySource> propertySources = new ArrayList<>();

    for (PropertySource<?> source : env.getPropertySources()) {
      if (source instanceof MapPropertySource && source.getName().contains("applicationConfig")) {
        propertySources.add((MapPropertySource) source);
      }
    }

    propertySources.stream()
      .map(propertySource -> propertySource.getSource().keySet())
      .flatMap(Collection::stream)
      .distinct()
      .sorted()
      .forEach(key -> {
        try {
          log.debug(key + "=" + env.getProperty(key));
        } catch (Exception e) {
          log.warn("{} -> {}", key, e.getMessage());
        }
      });
  }
}

相关文章