远程PropertySource [英] Remote PropertySource

查看:106
本文介绍了远程PropertySource的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

任何人都可以构造一个PropertySource,该PropertySource使用一个远程源(例如数据库)从中检索属性值.想法是构造一个PropertySource(需要一些连接信息,例如主机/端口),然后将其插入PropertySourcePlaceholderConfigurer.

Has anyone had any luck constructing a PropertySource that uses a remote source (for example a database) from which to retrieve property values. The idea would be to construct a PropertySource (needs some connection information such as host/port) and plug that into a PropertySourcePlaceholderConfigurer.

这个问题似乎是鸡肉和鸡蛋的问题.如何获取连接信息到PropertySource?我可以首先使用配置实例化PropertySourcePlaceholderConfigurer,以使用远程主机和端口属性加载属性文件,然后再实例化PropertySource并将其注入配置器.但是,我似乎无法找到一种方法来确保要实例化的第一个bean(并快速注入到配置程序中)是我的属性来源.我需要使用它,因为,当然,我所有其他的Bean都依赖于远程属性.

The problem seems to be a chicken and egg problem. How can I get the connection information down to the PropertySource? I could first instantiate the PropertySourcePlaceholderConfigurer with configuration to load a property file with the remote host and port properties and then later instantiate the PropertySource and inject that back into the configurer. However, I can't seem to figure a way to ensure that the very first bean to be instantiated (and quickly injected into the configurer) is my property source. I need to have this because, of course, all my other beans depend on the remote properties.

推荐答案

公用配置支持通过org.apache.commons.configuration.ConfigurationBuilder将来自各种来源(包括JDBC数据源)的属性加载到org.apache.commons.configuration.Configuration对象中.

Commons Configuration supports loading properties from a variety of sources (including JDBC Datasources) into a org.apache.commons.configuration.Configuration object via a org.apache.commons.configuration.ConfigurationBuilder.

使用org.apache.commons.configuration.ConfiguratorConverter,可以将Configuration对象转换为java.util.Properties对象,该对象可以传递给PropertySourcesPlaceholderConfigurer.

Using the org.apache.commons.configuration.ConfiguratorConverter, you can convert the Configuration object into a java.util.Properties object which can be passed to the PropertySourcesPlaceholderConfigurer.

关于如何配置ConfigurationBuilder的鸡和蛋的问题,我建议使用org.springframework.core.env.Environment来查询系统属性,命令行属性或JNDI属性.

As to the chicken and egg question of how to configure the ConfigurationBuilder, I recommend using the org.springframework.core.env.Environment to query for system properties, command-line properties or JNDI properties.

在此示例中:

@Configuration
public class RemotePropertyConfig {

   @Bean
   public static PropertySourcesPlaceholderConfigurer propertyPlaceholderConfigurer(Environment environment)
    throws Exception {
    final PropertySourcesPlaceholderConfigurer props = new PropertySourcesPlaceholderConfigurer();
    final ConfigurationBuilder configurationBuilder = new DefaultConfigurationBuilder(environment.getProperty("configuration.definition.file"));
    props.setProperties(ConfigurationConverter.getProperties(configurationBuilder.getConfiguration()));
    return props;
}

您将需要指定环境属性configuration.definition.file,该属性指向配置Commons Configuration所需的文件:

You will need to specify the environment property configuration.definition.file which points to a file needed to configure Commons Configuration:

这篇关于远程PropertySource的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆