简体   繁体   中英

how to call a method after Environment object initialized in Spring and java?

i have a class which contains reading a property file through spring.

Property file name sample.properties:
  Properties contains:
         a=A1
         b=B1
         etc.,

So i am reading easily through env variable. So i need this thing two purpose , One is Key Value and second is Value as key , key as Value(vice-versa) Hence, i added a method to put key as vaule and value as key in Map. The problem, its not putting values in map, so everything null. i am guessing @postConstruct is may be problem? and PropLoaderUtils class nowhere added in bean file. But howvever, i am getting value in env .

@Component
@Configuration
@PropertySource(name = "props", value = {
        "classpath:config/sample.properties"
        })
public class PropLoaderUtils {
    @Autowired
    private Environment env;

    private Map<String, String> valueAsKeyMap = new HashMap<String, String>();


    @PostConstruct
    private void setPropertyKeys() {
        Iterator<?> itr = ((AbstractEnvironment) env).getPropertySources().iterator();
        Map<String, Object> map = new HashMap<String, Object>();
        while (itr.hasNext()) {
            PropertySource propertySource = (PropertySource) itr.next();
            if (propertySource instanceof MapPropertySource) {
                map.putAll(((MapPropertySource) propertySource).getSource());
            }
        }

        for (Entry<String, Object> entry : map.entrySet()) {
            valueAsKeyMap.put(entry.getValue().toString(),entry.getKey());

        }
    }
    public String getProperty(String key) {
        String prop = env.getProperty(key);
        return prop;
    }

    public String getPropertyKeyAsValues(String value) {
        return valueAsKeyMap.getProperty(value);

    }


}

It is working fine after few changes. please find below :

    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.core.env.Environment;
    import java.util.*;
    import java.util.Map.Entry;
    import org.springframework.core.env.AbstractEnvironment;
    import org.springframework.core.env.MapPropertySource;
    import javax.annotation.PostConstruct;


    @Configuration
    @PropertySource(value = {
            "classpath:config/sample.properties"
            })
    public class PropLoaderUtils {
        @Autowired
        private Environment env;

        private Map<String, String> valueAsKeyMap = new HashMap<String, String>();


        @PostConstruct
        private void setPropertyKeys() {
            Iterator<?> itr = ((AbstractEnvironment) env).getPropertySources().iterator();
            Map<String, Object> map = new HashMap<String, Object>();
            while (itr.hasNext()) {
                org.springframework.core.env.PropertySource propertySource = (org.springframework.core.env.PropertySource) itr.next();
                if (propertySource instanceof MapPropertySource) {
                    map.putAll(((MapPropertySource) propertySource).getSource());
                }
            }

            for (Entry<String, Object> entry : map.entrySet()) {
                valueAsKeyMap.put(entry.getKey(), entry.getValue().toString());

            }

            valueAsKeyMap.entrySet().forEach(entry -> System.out.println("key "+entry.getKey()+" value "+entry.getValue()));;
        }


}

Output:

key a value A1
key b value B1

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