Example usage for org.springframework.context.support PropertySourcesPlaceholderConfigurer setPropertySources

List of usage examples for org.springframework.context.support PropertySourcesPlaceholderConfigurer setPropertySources

Introduction

In this page you can find the example usage for org.springframework.context.support PropertySourcesPlaceholderConfigurer setPropertySources.

Prototype

public void setPropertySources(PropertySources propertySources) 

Source Link

Document

Customize the set of PropertySources to be used by this configurer.

Usage

From source file:org.greencheek.utils.environment.propertyplaceholder.examples.spring.profile.AppBootstrap.java

@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
    PropertySourcesPlaceholderConfigurer config = new PropertySourcesPlaceholderConfigurer();

    MutablePropertySources sources = new MutablePropertySources();
    sources.addFirst(new PropertiesPropertySource("p", environmentalProperties));

    config.setPropertySources(sources);

    return config;
}

From source file:org.springframework.cloud.aws.context.config.xml.ContextCredentialsBeanDefinitionParserTest.java

@Test
public void parseBean_withProfileCredentialsProviderAndProfileFile_createProfileCredentialsProvider()
        throws IOException {
    GenericApplicationContext applicationContext = new GenericApplicationContext();

    Map<String, Object> secretAndAccessKeyMap = new HashMap<>();
    secretAndAccessKeyMap.put("profilePath",
            new ClassPathResource(getClass().getSimpleName() + "-profile", getClass()).getFile()
                    .getAbsolutePath());

    applicationContext.getEnvironment().getPropertySources()
            .addLast(new MapPropertySource("test", secretAndAccessKeyMap));
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setPropertySources(applicationContext.getEnvironment().getPropertySources());

    XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(applicationContext);
    reader.loadBeanDefinitions(new ClassPathResource(
            getClass().getSimpleName() + "-profileCredentialsProviderWithFile.xml", getClass()));

    applicationContext.refresh();/*w ww .  ja  va 2  s . c o m*/

    AWSCredentialsProvider provider = applicationContext.getBean(
            AmazonWebserviceClientConfigurationUtils.CREDENTIALS_PROVIDER_BEAN_NAME,
            AWSCredentialsProvider.class);
    assertNotNull(provider);

    assertEquals("testAccessKey", provider.getCredentials().getAWSAccessKeyId());
    assertEquals("testSecretKey", provider.getCredentials().getAWSSecretKey());
}

From source file:org.springframework.cloud.aws.context.config.annotation.ContextCredentialsConfigurationRegistrarTest.java

@Test
public void credentialsProvider_configWithAccessAndSecretKeyAsPlaceHolders_staticAwsCredentialsProviderConfiguredWithResolvedPlaceHolders()
        throws Exception {
    //Arrange//from  w  w w.j  av a 2 s .c  om
    this.context = new AnnotationConfigApplicationContext();

    Map<String, Object> secretAndAccessKeyMap = new HashMap<>();
    secretAndAccessKeyMap.put("accessKey", "accessTest");
    secretAndAccessKeyMap.put("secretKey", "testSecret");

    this.context.getEnvironment().getPropertySources()
            .addLast(new MapPropertySource("test", secretAndAccessKeyMap));
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setPropertySources(this.context.getEnvironment().getPropertySources());

    this.context.getBeanFactory().registerSingleton("configurer", configurer);
    this.context.register(ApplicationConfigurationWithAccessKeyAndSecretKeyAsPlaceHolder.class);
    this.context.refresh();
    //Act
    AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AWSCredentialsProvider.class);

    //Assert
    assertNotNull(awsCredentialsProvider);

    @SuppressWarnings("unchecked")
    List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils
            .getField(awsCredentialsProvider, "credentialsProviders");
    assertEquals(1, credentialsProviders.size());
    assertTrue(StaticCredentialsProvider.class.isInstance(credentialsProviders.get(0)));

    assertEquals("accessTest", awsCredentialsProvider.getCredentials().getAWSAccessKeyId());
    assertEquals("testSecret", awsCredentialsProvider.getCredentials().getAWSSecretKey());
}

From source file:org.springframework.cloud.aws.context.config.annotation.ContextCredentialsConfigurationRegistrarTest.java

@Test
public void credentialsProvider_configWithProfileNameAndCustomProfilePath_profileCredentialsProviderConfigured()
        throws Exception {
    //Arrange/*  w ww .  ja v  a 2  s  .co m*/
    this.context = new AnnotationConfigApplicationContext();

    Map<String, Object> secretAndAccessKeyMap = new HashMap<>();
    secretAndAccessKeyMap.put("profilePath",
            new ClassPathResource(getClass().getSimpleName() + "-profile", getClass()).getFile()
                    .getAbsolutePath());

    this.context.getEnvironment().getPropertySources()
            .addLast(new MapPropertySource("test", secretAndAccessKeyMap));
    PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
    configurer.setPropertySources(this.context.getEnvironment().getPropertySources());

    this.context.getBeanFactory().registerSingleton("configurer", configurer);
    this.context.register(ApplicationConfigurationWithProfileAndCustomProfilePath.class);
    this.context.refresh();

    //Act
    AWSCredentialsProvider awsCredentialsProvider = this.context.getBean(AWSCredentialsProvider.class);

    //Assert
    assertNotNull(awsCredentialsProvider);

    @SuppressWarnings("unchecked")
    List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils
            .getField(awsCredentialsProvider, "credentialsProviders");
    assertEquals(1, credentialsProviders.size());
    assertTrue(ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(0)));

    ProfileCredentialsProvider provider = (ProfileCredentialsProvider) credentialsProviders.get(0);
    assertEquals("testAccessKey", provider.getCredentials().getAWSAccessKeyId());
    assertEquals("testSecretKey", provider.getCredentials().getAWSSecretKey());
}