Package org.springframework.core.env

Examples of org.springframework.core.env.MutablePropertySources


    bf.registerBeanDefinition("testBean",
        genericBeanDefinition(TestBean.class)
          .addPropertyValue("name", "${my.name}")
          .getBeanDefinition());

    MutablePropertySources propertySources = new MutablePropertySources();
    propertySources.addLast(new MockPropertySource());

    PropertySourcesPlaceholderConfigurer pc = new PropertySourcesPlaceholderConfigurer();
    pc.setPropertySources(propertySources);
    pc.setProperties(new Properties() {{
      put("my.name", "local");
View Full Code Here


*/
public class MutablePropertySourcesTests {

  @Test
  public void test() {
    MutablePropertySources sources = new MutablePropertySources();
    sources.addLast(new MockPropertySource("b").withProperty("p1", "bValue"));
    sources.addLast(new MockPropertySource("d").withProperty("p1", "dValue"));
    sources.addLast(new MockPropertySource("f").withProperty("p1", "fValue"));

    assertThat(sources.size(), equalTo(3));
    assertThat(sources.contains("a"), is(false));
    assertThat(sources.contains("b"), is(true));
    assertThat(sources.contains("c"), is(false));
    assertThat(sources.contains("d"), is(true));
    assertThat(sources.contains("e"), is(false));
    assertThat(sources.contains("f"), is(true));
    assertThat(sources.contains("g"), is(false));

    assertThat(sources.get("b"), not(nullValue()));
    assertThat(sources.get("b").getProperty("p1"), equalTo((Object)"bValue"));
    assertThat(sources.get("d"), not(nullValue()));
    assertThat(sources.get("d").getProperty("p1"), equalTo((Object)"dValue"));

    sources.addBefore("b", new MockPropertySource("a"));
    sources.addAfter("b", new MockPropertySource("c"));

    assertThat(sources.size(), equalTo(5));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));

    sources.addBefore("f", new MockPropertySource("e"));
    sources.addAfter("f", new MockPropertySource("g"));

    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
    assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));

    sources.addLast(new MockPropertySource("a"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("e")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(4));
    assertThat(sources.precedenceOf(PropertySource.named("g")), is(5));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(6));

    sources.addFirst(new MockPropertySource("a"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));
    assertThat(sources.precedenceOf(PropertySource.named("d")), is(3));
    assertThat(sources.precedenceOf(PropertySource.named("e")), is(4));
    assertThat(sources.precedenceOf(PropertySource.named("f")), is(5));
    assertThat(sources.precedenceOf(PropertySource.named("g")), is(6));

    assertEquals(sources.remove("a"), PropertySource.named("a"));
    assertThat(sources.size(), equalTo(6));
    assertThat(sources.contains("a"), is(false));

    assertEquals(sources.remove("a"), null);
    assertThat(sources.size(), equalTo(6));

    String bogusPS = "bogus";
    try {
      sources.addAfter(bogusPS, new MockPropertySource("h"));
      fail("expected non-existent PropertySource exception");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(),
          equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
    }

    sources.addFirst(new MockPropertySource("a"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));

    sources.replace("a", new MockPropertySource("a-replaced"));
    assertThat(sources.size(), equalTo(7));
    assertThat(sources.precedenceOf(PropertySource.named("a-replaced")), is(0));
    assertThat(sources.precedenceOf(PropertySource.named("b")), is(1));
    assertThat(sources.precedenceOf(PropertySource.named("c")), is(2));

    sources.replace("a-replaced", new MockPropertySource("a"));

    try {
      sources.replace(bogusPS, new MockPropertySource("bogus-replaced"));
      fail("expected non-existent PropertySource exception");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(),
          equalTo(format(NON_EXISTENT_PROPERTY_SOURCE_MESSAGE, bogusPS)));
    }

    try {
      sources.addBefore("b", new MockPropertySource("b"));
      fail("expected exception");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(),
          equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
    }

    try {
      sources.addAfter("b", new MockPropertySource("b"));
      fail("expected exception");
    } catch (IllegalArgumentException ex) {
      assertThat(ex.getMessage(),
          equalTo(format(ILLEGAL_RELATIVE_ADDITION_MESSAGE, "b")));
    }
View Full Code Here

    }
  }

  @Test
  public void getNonExistentPropertySourceReturnsNull() {
    MutablePropertySources sources = new MutablePropertySources();
    assertThat(sources.get("bogus"), nullValue());
  }
View Full Code Here

     */
    @Override
    public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
        if (propertyResolver == null) {
            try {
                MutablePropertySources sources = new MutablePropertySources();
                PropertySource<?> localPropertySource = new PropertiesPropertySource(EXTENDED_PROPERTIES_SOURCE, mergeProperties());
                sources.addLast(localPropertySource);

                propertyResolver = new PropertySourcesPropertyResolver(sources);

            } catch (IOException e) {
                throw new BeanInitializationException("Could not load properties", e);
View Full Code Here

  @SuppressWarnings("unchecked")
  private void bindAndValidate(Map<String, String> raw) throws BindException {
    DataBinder dataBinder = new DataBinder(beanWrapper.getWrappedInstance());
    dataBinder.setIgnoreUnknownFields(false);
    dataBinder.setConversionService(conversionService);
    MutablePropertySources mps = new MutablePropertySources();
    mps.addFirst(new MapPropertySource("options", (Map) raw));
    try {
      dataBinder.bind(new PropertySourcesPropertyValues(mps));
    }
    catch (InvalidPropertyException e) {
      dataBinder.getBindingResult().addError(new FieldError("options", e.getPropertyName(), e.getMessage()));
View Full Code Here

public class PropertyMockingApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        MutablePropertySources propertySources = applicationContext.getEnvironment().getPropertySources();
        MockPropertySource mockEnvVars = new MockPropertySource().withProperty("bundling.enabled", false);
        propertySources.replace(StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME, mockEnvVars);
    }
View Full Code Here

    int port = SocketUtils.findAvailableTcpPort();

    @Bean
    public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() throws BindException {
      PropertySourcesPlaceholderConfigurer pspc = new PropertySourcesPlaceholderConfigurer();
      MutablePropertySources ps = new MutablePropertySources();
      ps.addFirst(optionsMetadataPropertySource());
      pspc.setPropertySources(ps);
      return pspc;
    }
View Full Code Here

   * @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(name
            + "-" + args.hashCode(), args));
        composite.addPropertySource(source);
        sources.replace(name, composite);
      }
      else {
        sources.addFirst(new SimpleCommandLinePropertySource(args));
      }
    }
  }
View Full Code Here

    resolvers.add(getVersionResolver(sourceClass));
    return resolvers;
  }

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

    Properties properties = new Properties();
    addWithPrefix(properties, getPropertiesFromApplication(environment),
        "vcap.application.");
    addWithPrefix(properties, getPropertiesFromServices(environment),
        "vcap.services.");
    MutablePropertySources propertySources = environment.getPropertySources();
    if (propertySources
        .contains(CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME)) {
      propertySources.addAfter(
          CommandLinePropertySource.COMMAND_LINE_PROPERTY_SOURCE_NAME,
          new PropertiesPropertySource("vcap", properties));
    }
    else {
      propertySources.addFirst(new PropertiesPropertySource("vcap", properties));
    }
  }
View Full Code Here

TOP

Related Classes of org.springframework.core.env.MutablePropertySources

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.