首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >远程PropertySource

远程PropertySource
EN

Stack Overflow用户
提问于 2014-08-12 18:29:06
回答 2查看 6K关注 0票数 5

是否有人成功地构建了一个使用远程源(例如数据库)来检索属性值的PropertySource。其想法是构建一个PropertySource (需要一些连接信息,比如主机/端口)并将其插入到PropertySourcePlaceholderConfigurer中。

问题似乎是鸡和蛋的问题。如何才能将连接信息下载到PropertySource?我可以先用配置实例化PropertySourcePlaceholderConfigurer,然后用远程主机和端口属性加载属性文件,然后实例化PropertySource并将其注入配置程序。但是,我似乎想不出一种方法来确保实例化的第一个bean (并迅速注入到配置程序中)是我的属性源。我需要这样做,因为当然,我的所有其他bean都依赖于远程属性。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-08-12 19:25:24

公域配置支持通过公域配置将各种源(包括JDBC )的属性加载到org.apache.commons.configuration.Configuration对象中。

使用org.apache.commons.configuration.ConfiguratorConverter,您可以将配置对象转换为可以传递给PropertySourcesPlaceholderConfigurer的java.util.Properties对象。

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

在这个例子中:

代码语言:javascript
复制
@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,它指向配置共用配置所需的文件:

票数 2
EN

Stack Overflow用户

发布于 2014-08-12 20:13:54

类似于上面Recardo的回答,我使用的是Spring的PropertiesLoaderUtils,而不是Apache的,但这等同于相同的东西。不太理想..。硬编码依赖注入,但嘿,它工作!

代码语言:javascript
复制
/**
 * This method must remain static as it's part of spring's initialization effort.
 * @return
 **/
@Bean
public static PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
  PropertySourcesPlaceholderConfigurer configurer = new PropertySourcesPlaceholderConfigurer();

  String dbHost = null;
  Integer dbPort = null;

  // check system / environment properties first
  Environment environment = new StandardEnvironment();
  if (environment.containsProperty(DB_HOST_KEY)) {
    dbHost = environment.getProperty(DB_HOST_KEY);
  }
  if (environment.containsProperty(DB_PORT_KEY)) {
    dbPort = Integer.valueOf(environment.getProperty(DB_PORT_KEY));
  }

  if (dbHost == null || dbPort == null) {
    // ok one or (probably) both properties null, let's go find the database.properties file
    Properties dbProperties;
    try {
      dbProperties = PropertiesLoaderUtils.loadProperties(new EncodedResource(new ClassPathResource("database.properties"), "UTF-8"));
    }
    catch (IOException e) {
      throw new RuntimeException("Could not load database.properties.  Please confirm the file is in the classpath");
    }

    if (dbHost == null) {
      dbHost = dbProperties.getProperty(DB_HOST_KEY);
    }
    if (dbPort == null) {
      dbPort = Integer.valueOf(dbProperties.getProperty(DB_PORT_KEY));
    }
  }

  PropertySourceService propertySourceService = new DBPropertySourceService(dbHost, dbPort);
  PropertySource<PropertySourceService> propertySource = new DBPropertySource(propertySourceService);
  MutablePropertySources propertySources = new MutablePropertySources();
  propertySources.addFirst(propertySource);
  configurer.setPropertySources(propertySources);

  return configurer;
}

对于每个请求,这里是远程属性源的源。这取决于一个“服务”类。好吧..。任何事..。远程访问套接字上的属性,与数据库对话,什么都可以。

代码语言:javascript
复制
/**
 * Property source for use with spring's PropertySourcesPlaceholderConfigurer where the source is a service
 * that connects to remote server for property values.
 **/
public class RemotePropertySource extends PropertySource<PropertySourceService> {

  private final Environment environment;
  /**
   * Constructor...
   * @param name
   * @param source
   **/
  public RemotePropertySource(PropertySourceService source) {
    super("RemotePropertySource", source);
    environment = new StandardEnvironment();
  }

  /* (non-Javadoc)
   * @see org.springframework.core.env.PropertySource#getProperty(java.lang.String)
   */
  @Override
  public Object getProperty(String name) {

    // check system / environment properties first
    String value;
    if (environment.containsProperty(name)) {
      value = environment.getProperty(name);
    }
    else {
      value = source.getProperty(name);
    }
    return value;
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25271537

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档