Package org.springframework.core.env

Examples of org.springframework.core.env.ConfigurableEnvironment


  }

  @Test
  public void overridePidFileWithSpring() throws Exception {
    File file = this.temporaryFolder.newFile();
    ConfigurableEnvironment environment = new StandardEnvironment();
    MockPropertySource propertySource = new MockPropertySource();
    propertySource.setProperty("spring.pidfile", file.getAbsolutePath());
    environment.getPropertySources().addLast(propertySource);
    ConfigurableApplicationContext context = mock(ConfigurableApplicationContext.class);
    given(context.getEnvironment()).willReturn(environment);
    ApplicationPreparedEvent event = new ApplicationPreparedEvent(
        new SpringApplication(), new String[] {}, context);
    ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
View Full Code Here


  }

  @Test
  public void differentEventTypes() throws Exception {
    File file = this.temporaryFolder.newFile();
    ConfigurableEnvironment environment = new StandardEnvironment();
    MockPropertySource propertySource = new MockPropertySource();
    propertySource.setProperty("spring.pidfile", file.getAbsolutePath());
    environment.getPropertySources().addLast(propertySource);
    ApplicationEnvironmentPreparedEvent event = new ApplicationEnvironmentPreparedEvent(
        new SpringApplication(), new String[] {}, environment);
    ApplicationPidFileWriter listener = new ApplicationPidFileWriter();
    listener.onApplicationEvent(event);
    assertThat(FileCopyUtils.copyToString(new FileReader(file)), isEmptyString());
View Full Code Here

   * @param propertySources The property sources. Required.
   * @return The given application's context.
   */
  public static ConfigurableApplicationContext configure(
      final ConfigurableApplicationContext context, final MutablePropertySources propertySources) {
    ConfigurableEnvironment env = configureEnvironment(context, propertySources);

    String modeProperty = env.getProperty(APP_MODE);
    if (StringUtils.isBlank(modeProperty)) {
      modeProperty = Mode.DEV.name();
      logger.warn("{} isn't set, using: {}", APP_MODE, modeProperty);
    } else {
      logger.info("{} is: {}", APP_MODE, modeProperty);
    }
    Mode mode = Mode.valueOf(modeProperty);

    // Activate the default profile
    env.setActiveProfiles(mode.name());

    complement(context, mode);

    return context;
  }
View Full Code Here

  public static ConfigurableEnvironment configureEnvironment(
      final ConfigurableApplicationContext context, final MutablePropertySources propertySources) {
    notNull(context, "The context is required.");
    notNull(propertySources, "The propertySources are required.");

    final ConfigurableEnvironment env = context.getEnvironment();
    if (propertySources.size() == 0) {
      logger.warn("No property files were found.");
    }
    // Add property's by precedence.
    MutablePropertySources mutablePropertySources = env.getPropertySources();
    for (PropertySource<?> propertySource : propertySources) {
      logger.debug("Adding property file: {}", propertySource);
      mutablePropertySources.addLast(propertySource);
    }
    // Move some less-used property source to the end of the chain.
View Full Code Here

   * @param applicationContext The application's context.
   * @return A {@link PropertiesComponent Camel Component}.
   */
  @SuppressWarnings("rawtypes")
  private static PropertiesComponent camelProperties(final ApplicationContext applicationContext) {
    ConfigurableEnvironment env = (ConfigurableEnvironment) applicationContext.getEnvironment();
    final MutablePropertySources propertySources = env.getPropertySources();
    List<String> names = new ArrayList<String>();
    for (PropertySource<?> propertySource : propertySources) {
      names.add(propertySource.getName());
    }

View Full Code Here

    private ClassPathXmlApplicationContext classPathXmlApplicationContext;

    @Test
    public void testSpringProfilesForDevEnvironment() {
        classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml");
        final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment();
        configurableEnvironment.setActiveProfiles("dev");
        classPathXmlApplicationContext.refresh();
        final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("devDatasourceConfig", DatasourceConfig.class);

        Assert.assertTrue(datasourceConfig instanceof DevDatasourceConfig);
    }
View Full Code Here

    }

    @Test
    public void testSpringProfilesForProdEnvironment() {
        classPathXmlApplicationContext = new ClassPathXmlApplicationContext("classpath:springProfiles-config.xml");
        final ConfigurableEnvironment configurableEnvironment = classPathXmlApplicationContext.getEnvironment();
        configurableEnvironment.setActiveProfiles("production");
        classPathXmlApplicationContext.refresh();
        final DatasourceConfig datasourceConfig = classPathXmlApplicationContext.getBean("productionDatasourceConfig", DatasourceConfig.class);

        Assert.assertTrue(datasourceConfig instanceof ProductionDatasourceConfig);
    }
View Full Code Here

    loadApplicationContext( child, moduleBootstrapConfig.getApplicationContextConfigurers() );
  }

  protected void loadApplicationContext( AcrossConfigurableApplicationContext context,
                                         Collection<ApplicationContextConfigurer> configurers ) {
    ConfigurableEnvironment environment = context.getEnvironment();

    for ( ApplicationContextConfigurer configurer : configurers ) {
      // First register property sources
      PropertySources propertySources = configurer.propertySources();

      if ( propertySources != null ) {
        for ( PropertySource<?> propertySource : propertySources ) {
          // Lower configurers means precedence in property sources
          environment.getPropertySources().addFirst( propertySource );
        }
      }
    }

    for ( ApplicationContextConfigurer configurer : configurers ) {
View Full Code Here

    assertEquals( "applicationContext", config.getProperty( "parentContextValue" ) );
  }

  @Test
  public void propertySourceOrder() {
    ConfigurableEnvironment env = (ConfigurableEnvironment) contextInfo.getModuleInfo( "directOnModule" )
                                                                       .getApplicationContext().getEnvironment();

    assertNotNull( env );

    MutablePropertySources sources = env.getPropertySources();
    assertNotNull( sources );
    assertEquals( 8, sources.size() );

    assertEquals( 4, sources.precedenceOf( sources.get( StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME ) ) );
    assertEquals( 5, sources.precedenceOf( sources.get( StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME ) ) );
View Full Code Here

  @Override
  public void setEnvironment( Environment environment ) {
    this.environment = environment;

    if ( environment instanceof ConfigurableEnvironment ) {
      ConfigurableEnvironment configurable = (ConfigurableEnvironment) environment;

      // Add defaults as the very last property source
      configurable.getPropertySources().addLast( registry );
    }
  }
View Full Code Here

TOP

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

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.