org.springframework.core.io.support.ResourcePropertySource Java Examples
The following examples show how to use
org.springframework.core.io.support.ResourcePropertySource.
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 File: EnviromentDiscovery.java From gazpachoquest with GNU General Public License v3.0 | 6 votes |
@Override public void initialize(ConfigurableApplicationContext ctx) { ConfigurableEnvironment environment = ctx.getEnvironment(); String activeProfiles[] = environment.getActiveProfiles(); if (activeProfiles.length == 0) { environment.setActiveProfiles("test,db_hsql"); } logger.info("Application running using profiles: {}", Arrays.toString(environment.getActiveProfiles())); String instanceInfoString = environment.getProperty(GAZPACHO_APP_KEY); String dbEngine = null; for (String profile : activeProfiles) { if (profile.startsWith("db_")) { dbEngine = profile; break; } } try { environment.getPropertySources().addLast( new ResourcePropertySource(String.format("classpath:/database/%s.properties", dbEngine))); } catch (IOException e) { throw new IllegalStateException(dbEngine + ".properties not found in classpath", e); } PropertySourcesPlaceholderConfigurer propertyHolder = new PropertySourcesPlaceholderConfigurer(); Map<String, String> environmentProperties = parseInstanceInfo(instanceInfoString); if (!environmentProperties.isEmpty()) { logger.info("Overriding default properties with {}", instanceInfoString); Properties properties = new Properties(); for (String key : environmentProperties.keySet()) { String value = environmentProperties.get(key); properties.put(key, value); } environment.getPropertySources().addLast(new PropertiesPropertySource("properties", properties)); propertyHolder.setEnvironment(environment); // ctx.addBeanFactoryPostProcessor(propertyHolder); // ctx.refresh(); } }
Example #2
Source File: PropertiesInitializer.java From proctor with Apache License 2.0 | 6 votes |
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
Source File: ExtendPropertySourcesBootstrap.java From thinking-in-spring-boot-samples with Apache License 2.0 | 6 votes |
public static void main(String[] args) { ConfigurableApplicationContext context = new SpringApplicationBuilder(ExtendPropertySourcesBootstrap.class) .web(WebApplicationType.NONE) // 非 Web 应用 .run(args); // 获取 Environment ConfigurableEnvironment environment = context.getEnvironment(); // 加载 META-INF/runtime.properties 配置文件 ResourcePropertySource propertySource = PropertySourceUtils.getResourcePropertySource("META-INF/runtime.properties"); // 追加至 PropertySources 顶端 environment.getPropertySources().addFirst(propertySource); // 读取 user.name 属性内容 System.out.println("从 Environment 读取属性 user.name = " + environment.getProperty("user.name")); System.out.println("Environment 所有 PropertySource : "); environment.getPropertySources().forEach(source -> { System.out.printf("\t %s\n", source); }); // 关闭上下文 context.close(); }
Example #4
Source File: LogFeederProps.java From ambari-logsearch with Apache License 2.0 | 5 votes |
@PostConstruct public void init() { properties = new Properties(); MutablePropertySources propSrcs = ((AbstractEnvironment) env).getPropertySources(); ResourcePropertySource propertySource = (ResourcePropertySource) propSrcs.get("class path resource [" + LogFeederConstants.LOGFEEDER_PROPERTIES_FILE + "]"); if (propertySource != null) { Stream.of(propertySource) .map(MapPropertySource::getPropertyNames) .flatMap(Arrays::<String>stream) .forEach(propName -> properties.setProperty(propName, env.getProperty(propName))); } else { throw new IllegalArgumentException("Cannot find logfeeder.properties on the classpath"); } }
Example #5
Source File: ChangelogCreator.java From salespoint with Apache License 2.0 | 5 votes |
private static RestTemplate setUpRestTemplate() throws IOException { FileSystemResource resource = new FileSystemResource("env.properties"); ResourcePropertySource propertySource = new ResourcePropertySource(resource); String username = propertySource.getProperty("github.username").toString(); String password = propertySource.getProperty("github.password").toString(); BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(username, password)); AuthCache authCache = new BasicAuthCache(); authCache.put(new HttpHost("api.github.com", 443, "https"), new BasicScheme()); final HttpClientContext context = HttpClientContext.create(); context.setCredentialsProvider(credentialsProvider); context.setAuthCache(authCache); ClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(HttpClientBuilder.create().build()) { @Override protected HttpContext createHttpContext(HttpMethod httpMethod, URI uri) { return context; } }; RestTemplate template = new RestTemplate(); template.setRequestFactory(factory); return template; }
Example #6
Source File: WebAnnoApplicationContextInitializer.java From webanno with Apache License 2.0 | 5 votes |
@Override public void initialize(ConfigurableApplicationContext aApplicationContext) { ConfigurableEnvironment aEnvironment = aApplicationContext.getEnvironment(); File settings = SettingsUtil.getSettingsFile(); // If settings were found, add them to the environment if (settings != null) { log.info("Settings: " + settings); try { aEnvironment.getPropertySources().addFirst( new ResourcePropertySource(new FileSystemResource(settings))); } catch (IOException e) { throw new IllegalStateException(e); } } // Activate bean profile depending on authentication mode if (AUTH_MODE_PREAUTH.equals(aEnvironment.getProperty(SettingsUtil.CFG_AUTH_MODE))) { aEnvironment.setActiveProfiles(PROFILE_PREAUTH); log.info("Authentication: pre-auth"); } else { aEnvironment.setActiveProfiles(PROFILE_DATABASE); log.info("Authentication: database"); } }
Example #7
Source File: YamlPropertySourceFactory.java From moon-api-gateway with MIT License | 5 votes |
@Override public PropertySource<?> createPropertySource(String name, EncodedResource resource) throws IOException { String filename = resource.getResource().getFilename(); if (filename != null && filename.endsWith(YML_FILE_EXTENSION)) { return name != null ? new YamlResourcePropertySource(name, resource) : new YamlResourcePropertySource(getNameForResource(resource.getResource()), resource); } return (name != null ? new ResourcePropertySource(name, resource) : new ResourcePropertySource(resource)); }
Example #8
Source File: CommonModule.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
/** * Listens for the {@link ApplicationEnvironmentPreparedEvent} and injects * ${c2mon.server.properties} into the environment with the highest precedence * (if it exists). This is in order to allow users to point to an external * properties file via ${c2mon.server.properties}. */ @Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); String propertySource = environment.getProperty("c2mon.server.properties"); if (propertySource != null) { try { environment.getPropertySources().addAfter("systemEnvironment", new ResourcePropertySource(propertySource)); } catch (IOException e) { throw new RuntimeException("Could not read property source", e); } } }
Example #9
Source File: C2monApplicationListener.java From c2mon with GNU Lesser General Public License v3.0 | 5 votes |
@Override public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { ConfigurableEnvironment environment = event.getEnvironment(); String propertySource = environment.getProperty("c2mon.client.conf.url"); if (propertySource != null) { try { environment.getPropertySources().addAfter("systemEnvironment", new ResourcePropertySource(propertySource)); } catch (IOException e) { throw new RuntimeException("Could not read property source", e); } } }
Example #10
Source File: ConfigurationClassParser.java From spring4-understanding with Apache License 2.0 | 5 votes |
private void addPropertySource(ResourcePropertySource propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); if (propertySources.contains(name) && this.propertySourceNames.contains(name)) { // We've already added a version, we need to extend it PropertySource<?> existing = propertySources.get(name); if (existing instanceof CompositePropertySource) { ((CompositePropertySource) existing).addFirstPropertySource(propertySource.withResourceName()); } else { if (existing instanceof ResourcePropertySource) { existing = ((ResourcePropertySource) existing).withResourceName(); } CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(propertySource.withResourceName()); composite.addPropertySource(existing); propertySources.replace(name, composite); } } else { if (this.propertySourceNames.isEmpty()) { propertySources.addLast(propertySource); } else { String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1); propertySources.addBefore(firstProcessed, propertySource); } } this.propertySourceNames.add(name); }
Example #11
Source File: ConfigurationClassParser.java From lams with GNU General Public License v2.0 | 5 votes |
private void addPropertySource(PropertySource<?> propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); if (propertySources.contains(name) && this.propertySourceNames.contains(name)) { // We've already added a version, we need to extend it PropertySource<?> existing = propertySources.get(name); PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? ((ResourcePropertySource) propertySource).withResourceName() : propertySource); if (existing instanceof CompositePropertySource) { ((CompositePropertySource) existing).addFirstPropertySource(newSource); } else { if (existing instanceof ResourcePropertySource) { existing = ((ResourcePropertySource) existing).withResourceName(); } CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, composite); } } else { if (this.propertySourceNames.isEmpty()) { propertySources.addLast(propertySource); } else { String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1); propertySources.addBefore(firstProcessed, propertySource); } } this.propertySourceNames.add(name); }
Example #12
Source File: JasyptConfiguration.java From seed with Apache License 2.0 | 5 votes |
@Override public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { MutablePropertySources propertySources = ((ConfigurableEnvironment)environment).getPropertySources(); for(org.springframework.core.env.PropertySource<?> obj : propertySources){ if(obj instanceof ResourcePropertySource){ propertySources.replace(obj.getName(), new PropertySourceWrapper((ResourcePropertySource)obj)); } } }
Example #13
Source File: DubboConfigConfigurationTest.java From dubbo-2.6.5 with Apache License 2.0 | 5 votes |
@Before public void before() throws IOException { context = new AnnotationConfigApplicationContext(); ResourcePropertySource propertySource = new ResourcePropertySource("META-INF/config.properties"); context.getEnvironment().getPropertySources().addFirst(propertySource); }
Example #14
Source File: InceptionApplicationContextInitializer.java From inception with Apache License 2.0 | 5 votes |
@Override public void initialize(ConfigurableApplicationContext aApplicationContext) { LoggingFilter.setLoggingUsername("SYSTEM"); ConfigurableEnvironment aEnvironment = aApplicationContext.getEnvironment(); File settings = SettingsUtil.getSettingsFile(); // If settings were found, add them to the environment if (settings != null) { log.info("Settings: " + settings); try { aEnvironment.getPropertySources().addFirst( new ResourcePropertySource(new FileSystemResource(settings))); } catch (IOException e) { throw new IllegalStateException(e); } } // Activate bean profile depending on authentication mode if (AUTH_MODE_PREAUTH.equals(aEnvironment.getProperty(SettingsUtil.CFG_AUTH_MODE))) { aEnvironment.setActiveProfiles(PROFILE_PREAUTH); log.info("Authentication: pre-auth"); } else { aEnvironment.setActiveProfiles(PROFILE_DATABASE); log.info("Authentication: database"); } }
Example #15
Source File: ResourcePropertySourceBootstrap.java From thinking-in-spring-boot-samples with Apache License 2.0 | 5 votes |
public static void main(String[] args) { // 调用工具类 PropertySourceUtils 获取 ResourcePropertySource ResourcePropertySource propertySource = PropertySourceUtils.getResourcePropertySource( "https://raw.githubusercontent.com/mercyblitz/tmp/master/remote.properties"); // 输出 Properties 内容 Stream.of(propertySource.getPropertyNames()) // 将 String 转化为 Stream 对象 .map(name -> name + " = " + propertySource.getProperty(name)) // 将 key 转化为 key = value 的形式 .forEach(System.out::println); }
Example #16
Source File: ExtendPropertySourcesApplicationContextInitializer.java From thinking-in-spring-boot-samples with Apache License 2.0 | 5 votes |
@Override public void initialize(ConfigurableApplicationContext applicationContext) { ConfigurableEnvironment environment = applicationContext.getEnvironment(); // 调用工具类 PropertySourceUtils 获取 ResourcePropertySource ResourcePropertySource propertySource = PropertySourceUtils.getResourcePropertySource("META-INF/initializer.properties"); // 添加至最高优先级 environment.getPropertySources().addFirst(propertySource); }
Example #17
Source File: ExtendPropertySourcesEnvironmentPostProcessor.java From thinking-in-spring-boot-samples with Apache License 2.0 | 5 votes |
@Override public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { String location = "https://raw.githubusercontent.com/mercyblitz/tmp/master/remote.properties"; // 调用工具类 PropertySourceUtils 获取 ResourcePropertySource ResourcePropertySource propertySource = PropertySourceUtils.getResourcePropertySource(location); // 添加至最高优先级 environment.getPropertySources().addFirst(propertySource); }
Example #18
Source File: ExtendPropertySourcesApplicationListener.java From thinking-in-spring-boot-samples with Apache License 2.0 | 5 votes |
public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { // 从事件获取 Environment 对象 ConfigurableEnvironment environment = event.getEnvironment(); // 调用工具类 PropertySourceUtils 获取 ResourcePropertySource ResourcePropertySource propertySource = PropertySourceUtils.getResourcePropertySource("META-INF/listener.properties"); // 添加至最高优先级 environment.getPropertySources().addFirst(propertySource); }
Example #19
Source File: ExtendPropertySourcesRunListener.java From thinking-in-spring-boot-samples with Apache License 2.0 | 5 votes |
@Override public void environmentPrepared(ConfigurableEnvironment environment) { // 调用工具类 PropertySourceUtils 获取 ResourcePropertySource ResourcePropertySource propertySource = PropertySourceUtils.getResourcePropertySource("/META-INF/run-listener.properties"); // 添加至最高优先级 environment.getPropertySources().addFirst(propertySource); }
Example #20
Source File: BisqEnvironment.java From bisq-core with GNU Affero General Public License v3.0 | 5 votes |
PropertySource<?> getAppDirProperties() throws Exception { Resource resource = getAppDirPropertiesResource(); if (!resource.exists()) return new PropertySource.StubPropertySource(BISQ_APP_DIR_PROPERTY_SOURCE_NAME); return new ResourcePropertySource(BISQ_APP_DIR_PROPERTY_SOURCE_NAME, resource); }
Example #21
Source File: ConfigurationClassParser.java From java-technology-stack with MIT License | 5 votes |
private void addPropertySource(PropertySource<?> propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); if (this.propertySourceNames.contains(name)) { // We've already added a version, we need to extend it PropertySource<?> existing = propertySources.get(name); if (existing != null) { PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? ((ResourcePropertySource) propertySource).withResourceName() : propertySource); if (existing instanceof CompositePropertySource) { ((CompositePropertySource) existing).addFirstPropertySource(newSource); } else { if (existing instanceof ResourcePropertySource) { existing = ((ResourcePropertySource) existing).withResourceName(); } CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, composite); } return; } } if (this.propertySourceNames.isEmpty()) { propertySources.addLast(propertySource); } else { String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1); propertySources.addBefore(firstProcessed, propertySource); } this.propertySourceNames.add(name); }
Example #22
Source File: ConfigurationClassParser.java From spring-analysis-note with MIT License | 5 votes |
private void addPropertySource(PropertySource<?> propertySource) { String name = propertySource.getName(); MutablePropertySources propertySources = ((ConfigurableEnvironment) this.environment).getPropertySources(); if (this.propertySourceNames.contains(name)) { // We've already added a version, we need to extend it PropertySource<?> existing = propertySources.get(name); if (existing != null) { PropertySource<?> newSource = (propertySource instanceof ResourcePropertySource ? ((ResourcePropertySource) propertySource).withResourceName() : propertySource); if (existing instanceof CompositePropertySource) { ((CompositePropertySource) existing).addFirstPropertySource(newSource); } else { if (existing instanceof ResourcePropertySource) { existing = ((ResourcePropertySource) existing).withResourceName(); } CompositePropertySource composite = new CompositePropertySource(name); composite.addPropertySource(newSource); composite.addPropertySource(existing); propertySources.replace(name, composite); } return; } } if (this.propertySourceNames.isEmpty()) { propertySources.addLast(propertySource); } else { String firstProcessed = this.propertySourceNames.get(this.propertySourceNames.size() - 1); propertySources.addBefore(firstProcessed, propertySource); } this.propertySourceNames.add(name); }
Example #23
Source File: JasyptConfiguration.java From seed with Apache License 2.0 | 4 votes |
PropertySourceWrapper(ResourcePropertySource propertySource) { super(propertySource.getName(), propertySource.getSource()); }
Example #24
Source File: TestPropertySourceUtils.java From spring4-understanding with Apache License 2.0 | 4 votes |
/** * 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); } }
Example #25
Source File: TestPropertySourceUtils.java From java-technology-stack with MIT License | 4 votes |
/** * Add the {@link Properties} files from the given resource {@code locations} * to the supplied {@link ConfigurableEnvironment environment}. * <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 environment the environment to update; never {@code null} * @param resourceLoader the {@code ResourceLoader} to use to load each resource; * never {@code null} * @param locations the resource locations of {@code Properties} files to add * to the environment; potentially empty but never {@code null} * @throws IllegalStateException if an error occurs while processing a properties file * @since 4.3 * @see ResourcePropertySource * @see TestPropertySource#locations * @see #addPropertiesFilesToEnvironment(ConfigurableApplicationContext, String...) */ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment, ResourceLoader resourceLoader, String... locations) { Assert.notNull(environment, "'environment' must not be null"); Assert.notNull(resourceLoader, "'resourceLoader' must not be null"); Assert.notNull(locations, "'locations' must not be null"); try { for (String location : locations) { String resolvedLocation = environment.resolveRequiredPlaceholders(location); Resource resource = resourceLoader.getResource(resolvedLocation); environment.getPropertySources().addFirst(new ResourcePropertySource(resource)); } } catch (IOException ex) { throw new IllegalStateException("Failed to add PropertySource to Environment", ex); } }
Example #26
Source File: TestPropertySourceUtils.java From spring-analysis-note with MIT License | 4 votes |
/** * Add the {@link Properties} files from the given resource {@code locations} * to the supplied {@link ConfigurableEnvironment environment}. * <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 environment the environment to update; never {@code null} * @param resourceLoader the {@code ResourceLoader} to use to load each resource; * never {@code null} * @param locations the resource locations of {@code Properties} files to add * to the environment; potentially empty but never {@code null} * @throws IllegalStateException if an error occurs while processing a properties file * @since 4.3 * @see ResourcePropertySource * @see TestPropertySource#locations * @see #addPropertiesFilesToEnvironment(ConfigurableApplicationContext, String...) */ public static void addPropertiesFilesToEnvironment(ConfigurableEnvironment environment, ResourceLoader resourceLoader, String... locations) { Assert.notNull(environment, "'environment' must not be null"); Assert.notNull(resourceLoader, "'resourceLoader' must not be null"); Assert.notNull(locations, "'locations' must not be null"); try { for (String location : locations) { String resolvedLocation = environment.resolveRequiredPlaceholders(location); Resource resource = resourceLoader.getResource(resolvedLocation); environment.getPropertySources().addFirst(new ResourcePropertySource(resource)); } } catch (IOException ex) { throw new IllegalStateException("Failed to add PropertySource to Environment", ex); } }