如何使用 Spring @Value 从 java 属性文件填充 HashMap [英] How to fill HashMap from java property file with Spring @Value

查看:37
本文介绍了如何使用 Spring @Value 从 java 属性文件填充 HashMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以使用 Spring @Value 将值从属性文件映射到 HashMap.

Is it possible to use Spring @Value, to map values from properties file to the HashMap.

目前我有这样的东西,映射一个值不是问题.但我需要在 HashMap 到期时映射自定义值.这样的事情可能吗?

Currently I have something like this, and mapping one value is not a problem. But I need to map custom values in HashMap expirations. Is something like this possible?

@Service
@PropertySource(value = "classpath:my_service.properties")
public class SomeServiceImpl implements SomeService {


    @Value("#{conf['service.cache']}")
    private final boolean useCache = false;

    @Value("#{conf['service.expiration.[<custom name>]']}")
    private final HashMap<String, String> expirations = new HashMap<String, String>();

属性文件:'my_service.properties'

Property file: 'my_service.properties'

service.cache=true
service.expiration.name1=100
service.expiration.name2=20

是否可以像这样映射key:value set

Is it posible to map like this key:value set

  • name1 = 100

  • name1 = 100

name2 = 20

推荐答案

我根据上一篇文章提出了一个解决方案.

I make one solution inspired by the previous post.

在Spring配置中注册属性文件:

Register property file in the Spring configuration:

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

然后我创建组件:

@Component("PropertyMapper")
public class PropertyMapper {

    @Autowired
    ApplicationContext applicationContext;

    public HashMap<String, Object> startWith(String qualifier, String startWith) {
        return startWith(qualifier, startWith, false);
    }

    public HashMap<String, Object> startWith(String qualifier, String startWith, boolean removeStartWith) {
        HashMap<String, Object> result = new HashMap<String, Object>();

        Object obj = applicationContext.getBean(qualifier);
        if (obj instanceof Properties) {
            Properties mobileProperties = (Properties)obj;

            if (mobileProperties != null) {
                for (Entry<Object, Object> e : mobileProperties.entrySet()) {
                    Object oKey = e.getKey();
                    if (oKey instanceof String) {
                        String key = (String)oKey;
                        if (((String) oKey).startsWith(startWith)) {
                            if (removeStartWith) 
                                key = key.substring(startWith.length());
                            result.put(key, e.getValue());
                        }
                    }
                }
            }
        }

        return result;
    }
}

当我想将所有以特定值开头的属性映射到 HashMap 时,使用 @Value 注释:

And when I want to map all properties that begin with specifix value to HashMap, with @Value annotation:

@Service
public class MyServiceImpl implements MyService {

    @Value("#{PropertyMapper.startWith('myProp', 'service.expiration.', true)}")
    private HashMap<String, Object> portalExpirations;

这篇关于如何使用 Spring @Value 从 java 属性文件填充 HashMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆