简体   繁体   中英

Spring boot get all properties from properties file and load into hashmap

I have a properties file that I want to load the contents into a hashmap, is this possible?

styles.properties

email.style.body=background: red; font-family: 'Helvetica', 'Arial', sans-serif; font-size: 12px; font-weight: 400; margin: 0 auto; padding: 0; line-height:22px
email.style.box=border-radius: 5px; background: #FFF; padding: 15px 20px 18px 20px; box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075) !important;
email.style.box.half=max-width: 360px;
email.style.box.title=font-family: Helvetica, Arial, sans-serif; font-weight: 700; font-style: normal; font-size: 12px; text-align: left; text-transform: uppercase; margin: 0 0 10px 0;
email.style.box.content=font-family: Helvetica, Arial, sans-serif; font-weight: 400; font-size: 13px; min-height: 150px !important; position: relative;
email.style.box.content.p=margin-block-start: 0; margin-block-end: 0;
email.style.box.content.attachments=width: 100%; position: absolute; bottom: 10px;

Currently, I am getting each value via @Value annotation, I am thinking if it is possible to get all these properties and put into a hashmap.

This way, I can just call map.get("email.style.box.content.attachments") to get a specific value

There is a possibility the this styles.properties will get big.

Manually, I can read the file... read each line and then split the key and value and store in hashmap.

This is possible but I think the practice is not good. Is there a better way to do this? Or what is the best approach to put all these properties into a hashmap?

TIA

Based on this questions / answers 1 , 2

Don't know if it is the good practice, but the below worked for me:

To access the custom styles.properties I had to annotate the StylesConfiguration with the @PropertySource . I've placed the styles.properties in the resources directory (next to the application.properties ).

@Configuration
@PropertySource("classpath:styles.properties")
public class StylesConfiguration {
    private final Map<String, Object> stylesMap = new HashMap<>();
    private static final String SEARCH_KEY = "[styles.properties]";

    public StylesConfiguration(ConfigurableEnvironment environment) {
        org.springframework.core.env.PropertySource<?> propertySource = null;
        for (org.springframework.core.env.PropertySource<?> propSource : environment.getPropertySources()) {
            if (propSource.getName().contains(SEARCH_KEY)) {
                propertySource = propSource;
                break;
            }
        }

        if (propertySource == null) {
            throw new RuntimeException(SEARCH_KEY + " not found");
        } else {
            loadStylesMap(propertySource);
        }
    }

    public Map<String, Object> getStylesMap() {
        return stylesMap;
    }

    private void loadStylesMap(org.springframework.core.env.PropertySource<?> propertySource) {
        EnumerablePropertySource<?> enumerablePropertySource = (EnumerablePropertySource<?>) propertySource;
//        System.out.println(Arrays.toString(((EnumerablePropertySource<?>) propertySource).getPropertyNames()));
        for (String name: enumerablePropertySource.getPropertyNames()) {
            stylesMap.put(name, enumerablePropertySource.getProperty(name));
        }
    }
}
@SpringBootApplication
public class Q63469791Application {

    public static void main(String[] args) {
        ApplicationContext app = SpringApplication.run(Q63469791Application .class, args);
        StylesConfiguration stylesConfiguration = app.getBean(StylesConfiguration.class);
        for (Map.Entry<String, Object> entry: stylesConfiguration.getStylesMap().entrySet()) {
            System.out.printf("key: %s, value %s\n", entry.getKey(), entry.getValue());
        }
    }
}

output:

email.style.body=background: red; font-family: 'Helvetica', 'Arial', sans-serif; font-size: 12px; font-weight: 400; margin: 0 auto; padding: 0; line-height:22px
email.style.box=border-radius: 5px; background: #FFF; padding: 15px 20px 18px 20px; box-shadow: 0 0.125rem 0.25rem rgba(0, 0, 0, 0.075) !important;
email.style.box.half=max-width: 360px;
email.style.box.title=font-family: Helvetica, Arial, sans-serif; font-weight: 700; font-style: normal; font-size: 12px; text-align: left; text-transform: uppercase; margin: 0 0 10px 0;
email.style.box.content=font-family: Helvetica, Arial, sans-serif; font-weight: 400; font-size: 13px; min-height: 150px !important; position: relative;
email.style.box.content.p=margin-block-start: 0; margin-block-end: 0;
email.style.box.content.attachments=width: 100%; position: absolute; bottom: 10px;

a good practice is to have a configuration class like

@ConfigurationProperties(prefix = "email") 
public class ConfigProperties { 
 
    private Style style;
 
    // standard getters and setters 
}

and a simple POJO like

public class Style {
    private String body;
    private String box;
    ...
 
    // standard getters and setters
}

you can read more here

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