下面列出了org.springframework.context.ConfigurableApplicationContext#getResource ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Path fileSystemConfig = Paths.get(SIMPLE_CONFIG);
boolean exists = Files.exists(fileSystemConfig);
if (!exists) {
InputStream classPathConfig = new ClassPathResource(SIMPLE_CONFIG).getInputStream();
Files.copy(classPathConfig, fileSystemConfig);
}
Resource resource = applicationContext.getResource("file:./" + SIMPLE_CONFIG);
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> propertySources = sourceLoader.load("simpleConfig", resource);
propertySources.forEach(propertySource -> applicationContext.getEnvironment().getPropertySources().addFirst(propertySource));
} catch (IOException e) {
throw new RuntimeException(e);
}
}
protected boolean tryAddPropertySource(final ConfigurableApplicationContext applicationContext,
final MutablePropertySources propSources,
final String filePath) {
if (filePath == null) {
return false;
}
Resource propertiesResource = applicationContext.getResource(filePath);
if (!propertiesResource.exists()) {
return false;
}
try {
ResourcePropertySource propertySource = new ResourcePropertySource(propertiesResource);
propSources.addFirst(propertySource);
} catch (IOException e) {
return false;
}
return true;
}
@Override
@SuppressWarnings("unchecked")
public void initialize(ConfigurableApplicationContext configurableApplicationContext) {
try {
Resource resource = configurableApplicationContext.getResource("classpath:configprops.json");
Map readValue = new ObjectMapper().readValue(resource.getInputStream(), Map.class);
Set<Map.Entry> set = readValue.entrySet();
List<MapPropertySource> propertySources = convertEntrySet(set, Optional.empty());
for (PropertySource propertySource : propertySources) {
configurableApplicationContext.getEnvironment()
.getPropertySources()
.addFirst(propertySource);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource("classpath:application-test.yml");
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> yamlTestProperties = sourceLoader.load("test-properties", resource);
applicationContext.getEnvironment().getPropertySources().addFirst(yamlTestProperties.get(0));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
try {
Resource resource = applicationContext.getResource("classpath:application.yml");
YamlPropertySourceLoader sourceLoader = new YamlPropertySourceLoader();
List<PropertySource<?>> yamlTestProperties =
sourceLoader.load("yamlTestProperties", resource);
for (PropertySource<?> ps : yamlTestProperties) {
applicationContext.getEnvironment().getPropertySources().addLast(ps);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
/**
* Add the {@link Properties} files from the given resource {@code locations}
* to the {@link Environment} of the supplied {@code context}.
* <p>Property placeholders in resource locations (i.e., <code>${...}</code>)
* will be {@linkplain Environment#resolveRequiredPlaceholders(String) resolved}
* against the {@code Environment}.
* <p>Each properties file will be converted to a {@link ResourcePropertySource}
* that will be added to the {@link PropertySources} of the environment with
* highest precedence.
* @param context the application context whose environment should be updated;
* never {@code null}
* @param locations the resource locations of {@code Properties} files to add
* to the environment; potentially empty but never {@code null}
* @since 4.1.5
* @see ResourcePropertySource
* @see TestPropertySource#locations
* @throws IllegalStateException if an error occurs while processing a properties file
*/
public static void addPropertiesFilesToEnvironment(ConfigurableApplicationContext context,
String[] locations) {
Assert.notNull(context, "context must not be null");
Assert.notNull(locations, "locations must not be null");
try {
ConfigurableEnvironment environment = context.getEnvironment();
for (String location : locations) {
String resolvedLocation = environment.resolveRequiredPlaceholders(location);
Resource resource = context.getResource(resolvedLocation);
environment.getPropertySources().addFirst(new ResourcePropertySource(resource));
}
}
catch (IOException e) {
throw new IllegalStateException("Failed to add PropertySource to Environment", e);
}
}