Java Code Examples for org.springframework.context.ConfigurableApplicationContext#getResource()

The following examples show how to use org.springframework.context.ConfigurableApplicationContext#getResource() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: Cleanstone   File: SimpleConfigLoader.java    License: MIT License 6 votes vote down vote up
@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);
    }
}
 
Example 2
Source Project: proctor   File: PropertiesInitializer.java    License: Apache License 2.0 6 votes vote down vote up
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;
}
 
Example 3
@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);
    }
}
 
Example 4
Source Project: Auth-service   File: CustomContextInitializer.java    License: MIT License 5 votes vote down vote up
@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);
    }
}
 
Example 5
@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);
  }
}
 
Example 6
/**
 * 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);
	}
}