简体   繁体   中英

Java.util.Properties with environment and Spring

I have many classes that consume a resource of type java.util.properties, but now, different property files have environment variables like:

com.something.one=AAA1
com.something.two=${MyEnvVar}
com.something.url=http://${myhost}:${myport}/xxx/yyy/zzz

my classes are like:

public class MyTest {

@Resource(name="utilProp")
private Properties  prop;

public void init() {
    System.out.println("var one = "+prop.getProperty("com.something.one"));
    System.out.println("var two = "+prop.getProperty("com.something.two"));
    System.out.println("var url = "+prop.getProperty("com.something.url"));

with this Bean:

<util:properties id="utilProp" location="classpath:myProp.properties"/>

I have this result:

var one = AAA1
var two = ${MyEnvVar}
var url = http://${myhost}:${myport}/xxx/yyy/zzz

How can I define a Bean that is compatible with Java.util.Properties but reads environment variables too?

Something like:

<beans:bean id="utilProp" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
  <beans:property name="searchSystemEnvironment" value="true"  /> //This not exists here !!! :(
  <beans:property name="locations">
     <beans:list>
       <beans:value>classpath:myProp.properties</beans:value>
     </beans:list>
   </beans:property>
</beans:bean>  

My solution was:


    import java.io.IOException;
    import java.util.Properties;
    import java.util.Set;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import org.springframework.beans.factory.config.PropertiesFactoryBean;
    
    public class MyPropertiesFactoryBean extends PropertiesFactoryBean {
    
        private  static Pattern p = Pattern.compile("\\$\\{([^\\}]+)\\}");
    
        @Override
        protected Properties createProperties() throws IOException {
            Properties props = mergeProperties();
            Set keys = props.stringPropertyNames();
            for (String key : keys) {
                String valor =  props.getProperty(key);
                Matcher m = p.matcher(valor); 
                if(m.find()) {
                    m.reset();
                    StringBuffer sb = new StringBuffer();
                    while(m.find()){
                        String envVarName = m.group(1);
                        String envVarValue = System.getenv(envVarName);
                        m.appendReplacement(sb, envVarValue == null ? "" : envVarValue);
                    }
                    m.appendTail(sb);
                    props.setProperty(key,sb.toString());
                }
            }
            return props;
        }
    }

try using below with correct path while running your app

java org.springframework.boot.loader.PropertiesLauncher --spring.config.location=file:myProp.properties 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM