Loading Properties with Spring (via System Properties)

前端 未结 3 666
暖寄归人
暖寄归人 2020-12-31 14:22

My problem is as follows:

I have server.properties for different environments. The path to those properties is provided trough a system property called

3条回答
  •  忘掉有多难
    2020-12-31 15:01

    Ok. I solved it. The problem is both of my PropertyPlaceholders are BeanFactoryPostProcessor those get processed after the context is loaded but the properties are set after. So it is impossible to populate one PropertyPlaceholder with another.

    Here is my solution in code ;-)

    package property.util;
    
    import org.apache.commons.lang.StringUtils;
    import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;
    import org.springframework.core.io.FileSystemResource;
    import org.springframework.core.io.Resource;
    
    import java.io.IOException;
    import java.util.Properties;
    
    /**
     * ConfigurablePropertyPlaceholder takes instructions which SystemProperty
     * contains the path to the propertyfile to load.
     *
     * @author Gabe Kaelin
     * 
     */
    public class ConfigurablePropertyPlaceholder extends PropertyPlaceholderConfigurer {
    
    
        private String propertyLocationSystemProperty;
        private String defaultPropertyFileName;
    
    
        public String getPropertyLocationSystemProperty() {
            return propertyLocationSystemProperty;
        }
    
        public void setPropertyLocationSystemProperty(String propertyLocationSystemProperty) {
            this.propertyLocationSystemProperty = propertyLocationSystemProperty;
        }
    
        public String getDefaultPropertyFileName() {
            return defaultPropertyFileName;
        }
    
        public void setDefaultPropertyFileName(String defaultPropertyFileName) {
            this.defaultPropertyFileName = defaultPropertyFileName;
        }
    
        /**
         * Overridden to fill the location with the path from the {@link #propertyLocationSystemProperty}
         *
         * @param props propeties instance to fill
         * @throws IOException
         */
    
        @Override
        protected void loadProperties(Properties props) throws IOException {
            Resource location = null;
            if(StringUtils.isNotEmpty(propertyLocationSystemProperty)){
    
                String propertyFilePath = System.getProperties().getProperty(propertyLocationSystemProperty);
                StringBuilder pathBuilder = new StringBuilder(propertyFilePath);
    
                if(StringUtils.isNotEmpty(defaultPropertyFileName) && !propertyFilePath.endsWith(defaultPropertyFileName)){
                    pathBuilder.append("/").append(defaultPropertyFileName);
                }
    
                location = new FileSystemResource(pathBuilder.toString());
            }
    
            setLocation(location);
            super.loadProperties(props);
        }
    }
    

    The according applicationContext.xml entry

    
      
      
      
    
    

    the java process can be started with

    java -DpropertyPath=/path/to/properties
    

    and it loads the properties and they are available in the applicationContext.xml

提交回复
热议问题