以 Map 或 Properties 对象的形式访问所有 Environment 属性 [英] Access all Environment properties as a Map or Properties object

查看:23
本文介绍了以 Map 或 Properties 对象的形式访问所有 Environment 属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用注释来配置我的 spring 环境,如下所示:

I am using annotations to configure my spring environment like this:

@Configuration
...
@PropertySource("classpath:/config/default.properties")
...
public class GeneralApplicationConfiguration implements WebApplicationInitializer 
{
    @Autowired
    Environment env;
}

这导致我的 default.properties 属性成为 Environment 的一部分.我想在这里使用 @PropertySource 机制,因为它已经提供了根据环境设置(例如 config_dir 位置)通过多个回退层和不同动态位置重载属性的可能性.我只是去掉了回退以使示例更容易.

This leads to my properties from default.properties being part of the Environment. I want to use the @PropertySource mechanism here, because it already provides the possibility to overload properties through several fallback layers and different dynamic locations, based on the environment settings (e.g. config_dir location). I just stripped the fallback to make the example easier.

但是,我现在的问题是我想在 default.properties 中配置例如我的数据源属性.您可以将设置传递给数据源,而无需使用

However, my problem now is that I want to configure for example my datasource properties in default.properties. You can pass the settings to the datasource without knowing in detail what settings the datasource expects using

Properties p = ...
datasource.setProperties(p);

然而,问题是,Environment 对象既不是 Properties 对象,也不是 Map 对象,也不是任何可比较的对象.在我看来,根本不可能访问环境的所有值,因为没有 keySetiterator 方法或任何类似的方法.

However, the problem is, the Environment object is neither a Properties object nor a Map nor anything comparable. From my point of view it is simply not possible to access all values of the environment, because there is no keySet or iterator method or anything comparable.

Properties p <=== Environment env?

我错过了什么吗?是否可以以某种方式访问​​ Environment 对象的所有条目?如果是,我可以将条目映射到 MapProperties 对象,我什至可以通过前缀过滤或映射它们 - 创建子集作为标准的 java Map ... 这就是我想做的.有什么建议吗?

Am I missing something? Is it possible to access all entries of the Environment object somehow? If yes, I could map the entries to a Map or Properties object, I could even filter or map them by prefix - create subsets as a standard java Map ... This is what I would like to do. Any suggestions?

推荐答案

你需要这样的东西,也许可以改进.这是第一次尝试:

You need something like this, maybe it can be improved. This is a first attempt:

...
import org.springframework.core.env.PropertySource;
import org.springframework.core.env.AbstractEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
...

@Configuration
...
@org.springframework.context.annotation.PropertySource("classpath:/config/default.properties")
...
public class GeneralApplicationConfiguration implements WebApplicationInitializer 
{
    @Autowired
    Environment env;

    public void someMethod() {
        ...
        Map<String, Object> map = new HashMap();
        for(Iterator it = ((AbstractEnvironment) env).getPropertySources().iterator(); it.hasNext(); ) {
            PropertySource propertySource = (PropertySource) it.next();
            if (propertySource instanceof MapPropertySource) {
                map.putAll(((MapPropertySource) propertySource).getSource());
            }
        }
        ...
    }
...

基本上,环境中的所有内容都是 MapPropertySource(并且有很多实现)都可以作为属性的 Map 访问.

Basically, everything from the Environment that's a MapPropertySource (and there are quite a lot of implementations) can be accessed as a Map of properties.

这篇关于以 Map 或 Properties 对象的形式访问所有 Environment 属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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