Package org.springframework.core.env

Examples of org.springframework.core.env.ConfigurableEnvironment


  @Test
  public void propertySourceOrder() throws Exception {
    SimpleNamingContextBuilder.emptyActivatedContextBuilder();

    ConfigurableEnvironment env = new StandardServletEnvironment();
    MutablePropertySources sources = env.getPropertySources();

    assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONFIG_PROPERTY_SOURCE_NAME)), equalTo(0));
    assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME)), equalTo(1));
    assertThat(sources.precedenceOf(PropertySource.named(StandardServletEnvironment.JNDI_PROPERTY_SOURCE_NAME)), equalTo(2));
    assertThat(sources.precedenceOf(PropertySource.named(StandardEnvironment.SYSTEM_PROPERTIES_PROPERTY_SOURCE_NAME)), equalTo(3));
View Full Code Here


  }

  @Test
  public void testWithInactiveProfile() {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
    ConfigurableEnvironment env = new StandardEnvironment();
    env.setActiveProfiles("other");
    provider.setEnvironment(env);
    Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_PROFILE_PACKAGE);
    assertThat(containsBeanClass(candidates, ProfileAnnotatedComponent.class), is(false));
  }
View Full Code Here

  }

  @Test
  public void testWithActiveProfile() {
    ClassPathScanningCandidateComponentProvider provider = new ClassPathScanningCandidateComponentProvider(true);
    ConfigurableEnvironment env = new StandardEnvironment();
    env.setActiveProfiles(ProfileAnnotatedComponent.PROFILE_NAME);
    provider.setEnvironment(env);
    Set<BeanDefinition> candidates = provider.findCandidateComponents(TEST_PROFILE_PACKAGE);
    assertThat(containsBeanClass(candidates, ProfileAnnotatedComponent.class), is(true));
  }
View Full Code Here

   * @since 4.1
   */
  private void addResourcePropertySourcesToEnvironment(ConfigurableApplicationContext context,
      MergedContextConfiguration mergedConfig) {
    try {
      ConfigurableEnvironment environment = context.getEnvironment();
      String[] locations = mergedConfig.getPropertySourceLocations();
      for (String location : locations) {
        String resolvedLocation = environment.resolveRequiredPlaceholders(location);
        Resource resource = context.getResource(resolvedLocation);
        ResourcePropertySource ps = new ResourcePropertySource(resource);
        environment.getPropertySources().addFirst(ps);
      }
    }
    catch (IOException e) {
      throw new IllegalStateException("Failed to add PropertySource to Environment", e);
    }
View Full Code Here

    String propertySourceName = String.format("%s:%s",
        moduleDefinition.getType(), moduleDefinition.getName());
    // Load short name values into a throwaway env
    String path = String.format("%s%s/%s/", xdModuleConfigLocation, moduleDefinition.getType(),
        moduleDefinition.getName());
    ConfigurableEnvironment throwAwayEnvironment = loadPropertySources(path, moduleDefinition.getName());
    EnumerablePropertySource<?> nakedPS = (EnumerablePropertySource<?>) throwAwayEnvironment.getPropertySources().get(
        APPLICATION_CONFIGURATION_PROPERTIES);
    // Now transform them to their fully qualified form
    Map<String, Object> values = new HashMap<String, Object>();
    for (String name : nakedPS.getPropertyNames()) {
      values.put(fullyQualifiedKey(moduleDefinition, name), nakedPS.getProperty(name));
    }
    EnumerablePropertySource<?> modulePS = new MapPropertySource(propertySourceName, values);
    ConfigurableEnvironment moduleEnvironment = new StandardEnvironment();
    // Append the rootEnvironment
    moduleEnvironment.merge(rootEnvironment);
    // The global environment has been loaded by boot too and
    // its PS of interest was also named "applicationConfigurationProperties"
    moduleEnvironment.getPropertySources().addBefore(APPLICATION_CONFIGURATION_PROPERTIES, modulePS);
    return moduleEnvironment;
  }
View Full Code Here

  /**
   * Construct a new environment and use Spring Boot to populate its property sources using
   * {@link ConfigFileApplicationListener}.
   */
  private ConfigurableEnvironment loadPropertySources(final String searchLocation, final String baseName) {
    final ConfigurableEnvironment environment = new StandardEnvironment();
    environment.merge(parentEnvironment);
    new ConfigFileApplicationListener() {

      public void apply() {
        setSearchLocations(searchLocation);
        // We'd like to do 'setSearchNames(baseName)', but the environment property
        // has strong precedence and is already set for XD_CONFIG_NAME.
        Map<String, Object> singletonMap = Collections.singletonMap("spring.config.name",
            (Object) baseName);
        environment.getPropertySources().addFirst(
            new MapPropertySource("searchNamesOverride", singletonMap));
        addPropertySources(environment, resourceLoader);
      }
    }.apply();
    return environment;
View Full Code Here

   * {@link org.springframework.core.env.Environment} that knows how to resolve module options placeholders.
   */
  @Override
  public void initialize() {
    this.application.initializers(new ContextIdApplicationContextInitializer(this.toString()));
    ConfigurableEnvironment parentEnvironment = parent == null ? null
        : parent.getEnvironment();
    ModuleEnvironment environment = new ModuleEnvironment(moduleOptions.asPropertySource(), parentEnvironment);
    for (PropertySource<?> source : propertySources) {
      environment.getPropertySources().addFirst(source);
    }
View Full Code Here

        + "\n    For more information please visit:                   "
        + "\n    http://www.springsource.org/spring-integration       "
        + "\n" + HORIZONTAL_LINE);

    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    final ConfigurableEnvironment environment = context.getEnvironment();

    final String fromEmailAddress;
    final String toEmailAddress;
    final String subject;
    final String body;

    System.out.print("\nFrom which email address would you like to send a message?: ");
    fromEmailAddress = scanner.nextLine();

    System.out.print("To which email address would you like to send a message?: ");
    toEmailAddress = scanner.nextLine();

    System.out.print("What is the subject line?: ");
    subject = scanner.nextLine();

    System.out.print("What is the body of the message?: ");
    body = scanner.nextLine();

    if (!environment.containsProperty("accessKey")) {
      System.out.print("Please enter your access key: ");
      final String accessKey = scanner.nextLine();
      environment.getSystemProperties().put("accessKey", accessKey);
    }

    if (!environment.containsProperty("secretKey")) {
      System.out.print("Please enter your secret key: ");
      final String secretKey = scanner.nextLine();
      environment.getSystemProperties().put("secretKey", secretKey);
    }

    context.load("classpath:META-INF/spring/integration/*-context.xml");
    context.registerShutdownHook();
    context.refresh();
 
View Full Code Here

    System.out.println("\tq. Quit the application");
    System.out.print("Enter your choice: ");

    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    final ConfigurableEnvironment environment = context.getEnvironment();

    boolean usingWiser = false;

    String toEmailAddress;

    while (true) {
      final String input = scanner.nextLine();

      if("1".equals(input.trim())) {
        environment.setActiveProfiles("default");
        usingWiser = true;

        System.out.print("\nTo which email address would you like to send a message?: ");
        toEmailAddress = scanner.nextLine();

        break;
      } else if("2".equals(input.trim())) {
        environment.setActiveProfiles("aws");

        if (!environment.containsProperty("accessKey")) {
          System.out.print("\nPlease enter your access key: ");
          final String accessKey = scanner.nextLine();
          environment.getSystemProperties().put("accessKey", accessKey);
        }

        if (!environment.containsProperty("secretKey")) {
          System.out.print("\nPlease enter your secret key: ");
          final String secretKey = scanner.nextLine();
          environment.getSystemProperties().put("secretKey", secretKey);
        }
        System.out.print("\nTo which email address would you like to send a message?: ");
        toEmailAddress = scanner.nextLine();

        break;
View Full Code Here

            + "\n                                                         "
            + HORIZONTAL_LINE );
    }

    final GenericXmlApplicationContext context = new GenericXmlApplicationContext();
    final ConfigurableEnvironment environment = context.getEnvironment();

    System.out.println("What would you like to do?");
    System.out.println("\t1. Upload a file to Amazon S3");
    System.out.println("\t2. Poll files from Amazon S3");
    System.out.println("\tq. Quit the application");
    System.out.print(" > ");

    String filePath;

    while (true) {
      final String input = scanner.nextLine();

      if("1".equals(input.trim())) {

        System.out.println("Uploading to Amazon S3...");

        environment.setActiveProfiles("upload-to-s3");
        setupCredentials(environment, scanner);
        setupS3info(environment, scanner);

        context.load("classpath:META-INF/spring/integration/*-context.xml");
        context.registerShutdownHook();
        context.refresh();

        System.out.print("\nPlease enter the path to the file you want to upload: ");
        filePath = scanner.nextLine();

        final MessageChannel messageChannel = context.getBean("s3channel", MessageChannel.class);
        messageChannel.send(MessageBuilder.withPayload(new File(filePath)).build());

        break;

      }
      else if("2".equals(input.trim())) {

        System.out.println("Polling files from Amazon S3...");
        environment.setActiveProfiles("poll-s3");
        setupCredentials(environment, scanner);
        setupS3info(environment, scanner);

        context.load("classpath:META-INF/spring/integration/*-context.xml");
        context.registerShutdownHook();
 
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.