0

I am using spring3. I am loading property file as below.

<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

Above configuration is working fine without any issues.

Now i have to load one more properties file with same keys but with different values. I cannot change the key names in property files.

now i have one more porperty file as below.

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

Now how can i load multiple property files with same keys?

Thanks!

user755806
  • 6,565
  • 27
  • 106
  • 153

1 Answers1

1

if you need to spring load many properties files (diff names) with same key, bellow impl in spring boot cloud config client (using springCloudConfigServer to load files) and add prefix for props is:



    @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();
        }
    }


Works fine in /actuator/refresh and to use @Value('${you.propertiesname.your.key}') or @ConfigurationProperties(prefix = "you.propertiesname")

And works with another uses for getProperty env