【问题标题】:loading multiple property files in spring with same key value pairs?在spring中使用相同的键值对加载多个属性文件?
【发布时间】:2014-01-29 07:34:38
【问题描述】:

我正在使用spring3。我正在加载如下属性文件。

<bean id="propertyFactory"
        class="org.springframework.beans.factory.config.PropertiesFactoryBean">
        <property name="ignoreResourceNotFound" value="false" />
        <property name="locations">
            <list>
                <value>classpath:conf/app.properties</value>
            </list>
        </property>
    </bean>


app.properties
------------
user=xxx
pwd=xxx
host=xxx

以上配置运行良好,没有任何问题。

现在我必须再加载一个具有相同键但具有不同值的属性文件。我无法更改属性文件中的键名。

现在我还有一个如下的 porperty 文件。

 app.properties
    ------------
    user=abc
    pwd=xxx
    host=differenthost

现在如何使用相同的键加载多个属性文件?

谢谢!

【问题讨论】:

标签: java spring java-ee-7


【解决方案1】:

如果您需要使用相同的键弹簧加载许多属性文件(差异名称), 在spring boot cloud config客户端中实现波纹管(使用springCloudConfigServer加载文件)并为props添加前缀是:

@RefreshScope @Configuration public class CustomProperties implements ApplicationListener { private static final Logger LOGGER = LoggerFactory.getLogger(CustomProperties.class); private static final String BOOTSTRAP_PROPERTIES = "bootstrapProperties"; @Autowired private ConfigurableEnvironment configurableEnvironment; @PostConstruct public void loadPropertySources() { try { final Collection originTrackedMapPropertySources = new ArrayList(); for (PropertySource propertySource : configurableEnvironment.getPropertySources()) { if (BOOTSTRAP_PROPERTIES.equalsIgnoreCase(propertySource.getName()) && propertySource instanceof OriginTrackedCompositePropertySource) { OriginTrackedCompositePropertySource bootstrapProperties = (OriginTrackedCompositePropertySource) propertySource; for (PropertySource bootstrapPropertiesSource : bootstrapProperties.getPropertySources()) { OriginTrackedCompositePropertySource originTrackedCompositePropertySource = (OriginTrackedCompositePropertySource) bootstrapPropertiesSource; for (PropertySource propertySourceOriginTrackedCompositePropertySource : originTrackedCompositePropertySource.getPropertySources()) { OriginTrackedMapPropertySource originTrackedMapPropertySource = (OriginTrackedMapPropertySource) propertySourceOriginTrackedCompositePropertySource; final String originTrackedMapPropertySourceName = originTrackedMapPropertySource.getName(); final Map newPropertySource = new TreeMap(); String newPropertySourceName = originTrackedMapPropertySourceName.substring(originTrackedMapPropertySourceName.lastIndexOf('/') + 1, originTrackedMapPropertySourceName.lastIndexOf('.')); newPropertySourceName = newPropertySourceName.toLowerCase().replace('-', '.'); for (String propertyName : originTrackedMapPropertySource.getPropertyNames()) { final Object propertyValue = originTrackedMapPropertySource.getProperty(propertyName); newPropertySource.put(newPropertySourceName + "." + propertyName, propertyValue); } originTrackedMapPropertySources.add(new OriginTrackedMapPropertySource(newPropertySourceName, newPropertySource)); } } } } // for (OriginTrackedMapPropertySource originTrackedMapPropertySource : originTrackedMapPropertySources) { LOGGER.info("PropertySourceName Added: {}", originTrackedMapPropertySource.getName()); configurableEnvironment.getPropertySources().addLast(originTrackedMapPropertySource); } LOGGER.info("Finish properties add"); } catch (Exception e) { LOGGER.error("Ex on configure property sources", e); } } @Override public void onApplicationEvent(EnvironmentChangeEvent environmentChangeEvent) { loadPropertySources(); } }

/actuator/refresh 和使用@Value('${you.propertiesname.your.key}')@ConfigurationProperties(prefix = "you.propertiesname") 中工作正常

并与 getProperty env 的其他用途一起使用

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-23
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 2019-04-16
    • 2016-04-11
    • 2016-06-18
    相关资源
    最近更新 更多