使用相同的键值对在spring中加载多个属性文件?

时间:2014-01-29 07:34:38

标签: java spring java-ee-7

我正在使用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

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

谢谢!

1 个答案:

答案 0 :(得分:0)

如果您需要使用相同的键来弹簧加载许多属性文件(差异名称), 在春季启动云配置客户端(使用springCloudConfigServer加载文件)中添加以下内容并为props添加前缀是:

import images from '../assets/cover/images/*.jpg';
const file_name = "file-name"
<img src=`${images[file_name]}` />

@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}')

并与getProperty env的其他用途配合使用