当前位置: 首页 > 面试题库 >

如何使用Spring @Value从Java属性文件填充HashMap

闾丘山
2023-03-14
问题内容

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

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

@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”

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

是否可以像这样的键映射:值集

  • name1 = 100

  • name2 = 20


问题答案:

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

<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;
    }
}

当我想将所有以specifix值开头的属性映射到带有@Value注释的HashMap时:

@Service
public class MyServiceImpl implements MyService {

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


 类似资料:
  • 问题内容: 我建立了一个小型应用程序,可以读取Excel文件并为我创建所有必要的SQL语句。客户不时操纵Excel文件。 Excel文件的第一行包含一个标题,我在阅读这些行时需要将其转换为整数。例如,在生成SQL语句之前,标题“英语”需要翻译成“ 30”。(只是一些内部定义)。(您可以将其与DNS- IP地址映射中的人类可读域名进行比较)目前,我手动进行映射,但是我想通过一个很小的Spring配置

  • 我有一个Java类,它从JSON文件/数据库/其他东西加载我的应用程序的属性。 我已将配置为Spring上下文中的bean。 现在我想在spring上下文文件中使用中的一个值来表示不支持SpEL的内容,例如spring集成日志通道适配器,例如。 是否有一种方法可以设置可以使用SpEL填充的“属性”?

  • 问题内容: 我想使用该类填充一个。 我想将条目加载到文件中,然后将其复制到中。 之前,我曾经使用属性文件初始化,但是现在我已经定义了,并且只想在构造函数中对其进行初始化。 较早的方法: 但是现在,我有这个 如何将属性对象分配给此处? 问题答案: 如果我理解正确,则属性中的每个值都是代表整数的字符串。因此,代码如下所示:

  • 问题内容: 如何从文本文件填充? 问题答案: 非常模糊的问题。您是说要每行输入一个吗?如果是这样,则要使用BufferedReader之类的东西,请读取所有行,并将它们保存为String数组。创建一个新的JComboBox传入该String构造函数。

  • 问题内容: 使用诸如 可以分配具有某些属性的某些字段。 是否有更短,更简洁的形式来执行此操作? 问题答案: