下面列出了org.springframework.context.support.PropertySourcesPlaceholderConfigurer#setPropertySources ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreResourceNotFound(true);
configurer.setIgnoreUnresolvablePlaceholders(true);
MutablePropertySources propertySources = new MutablePropertySources();
MockPropertySource source = new MockPropertySource()
.withProperty("rabbitmq.host", "192.168.10.10")
.withProperty("rabbitmq.port", "5673")
.withProperty("rabbitmq.username", "jfw")
.withProperty("rabbitmq.password", "jfw")
.withProperty("rabbitmq.channel-cache-size", 100);
propertySources.addLast(source);
configurer.setPropertySources(propertySources);
return configurer;
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreResourceNotFound(true);
configurer.setIgnoreUnresolvablePlaceholders(true);
MutablePropertySources propertySources = new MutablePropertySources();
MockPropertySource source = new MockPropertySource()
.withProperty("rabbitmq.port", "invalid");
propertySources.addLast(source);
configurer.setPropertySources(propertySources);
return configurer;
}
@Bean
public static PropertySourcesPlaceholderConfigurer placeHolderConfigurer() {
PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();
configurer.setIgnoreResourceNotFound(true);
configurer.setIgnoreUnresolvablePlaceholders(true);
MutablePropertySources propertySources = new MutablePropertySources();
propertySources.addLast(new MockPropertySource());
configurer.setPropertySources(propertySources);
return configurer;
}
@Test
void credentialsProvider_configWithAccessAndSecretKeyAsPlaceHolders_staticAwsCredentialsProviderConfiguredWithResolvedPlaceHolders()
throws Exception {
// @checkstyle:on
// Arrange
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
assertThat(awsCredentialsProvider).isNotNull();
@SuppressWarnings("unchecked")
List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils
.getField(awsCredentialsProvider, "credentialsProviders");
assertThat(credentialsProviders.size()).isEqualTo(1);
assertThat(AWSStaticCredentialsProvider.class
.isInstance(credentialsProviders.get(0))).isTrue();
assertThat(awsCredentialsProvider.getCredentials().getAWSAccessKeyId())
.isEqualTo("accessTest");
assertThat(awsCredentialsProvider.getCredentials().getAWSSecretKey())
.isEqualTo("testSecret");
}
@Bean
public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer(
final ConfigurableEnvironment env)
{
final PropertySourcesPlaceholderConfigurer configurer =
new PropertySourcesPlaceholderConfigurer();
// https://jira.spring.io/browse/SPR-9631 may simplify this in
// future
final MutablePropertySources sources = new MutablePropertySources();
sources.addLast(createConfigPropertySource());
configurer.setPropertySources(sources);
configurer.setEnvironment(env);
return configurer;
}
@Test
void credentialsProvider_configWithProfileNameAndCustomProfilePath_profileCredentialsProviderConfigured()
throws Exception {
// Arrange
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
assertThat(awsCredentialsProvider).isNotNull();
@SuppressWarnings("unchecked")
List<CredentialsProvider> credentialsProviders = (List<CredentialsProvider>) ReflectionTestUtils
.getField(awsCredentialsProvider, "credentialsProviders");
assertThat(credentialsProviders.size()).isEqualTo(1);
assertThat(
ProfileCredentialsProvider.class.isInstance(credentialsProviders.get(0)))
.isTrue();
ProfileCredentialsProvider provider = (ProfileCredentialsProvider) credentialsProviders
.get(0);
assertThat(provider.getCredentials().getAWSAccessKeyId())
.isEqualTo("testAccessKey");
assertThat(provider.getCredentials().getAWSSecretKey())
.isEqualTo("testSecretKey");
}