问题描述
我正在使用注释来配置我的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
,也不是任何可比较的对象.在我看来,根本不可能访问环境的所有值,因为没有keySet
或iterator
方法或任何类似方法.
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
对象的所有条目?如果是,我可以将条目映射到Map
或Properties
对象,甚至可以通过前缀过滤或映射它们-创建子集作为标准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属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!