仅通过@Value注入属性映射 [英] Injecting a map of properties via @Value alone

查看:116
本文介绍了仅通过@Value注入属性映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用 @Values 注释填充值映射,而不在applicationContext.xml 中定义任何内容或任何其他XML文件。



我使用的是spring boot,它没有任何 XML文件,也不需要任何XML文件,所以请不要告诉我用XML等声明任何属性读取器bean。



此外,这是属性注入问题 - 请不要建议使用数据库来存储数据 - 这不是答案,也不适用于我的情况。



另外,我也不能使用YAML(由于部署/运营要求)。



我已经尝试声明这次注射:

  @Value($ {myprop} )
Map< Integer,String>地图;

这一个

  @Value($ {myprop。*})
Map< Integer,String>地图;这些条目 application.properties

  myprop.1 =一个
myprop.2 =两个
myprop.3 =三个

然后尝试

  myprop [1] =一个
myprop [2] =两个
myprop [3] =三个

但不好 - 只是爆炸


无法自动装配字段:...无法解析占位符'myprop '


我找到了一个注入 String [] 的解决方法指定为 key1:value1,key2:value2,... 然后我在代码中解析,但我宁愿不这样做因为a)它的代码更多,并且b)列表将会很长,并且一行上的所有对将难以阅读和维护。



有没有办法自动构建一个从几个属性映射?



我不关心属性名称是什么,字段类型是什么或注释是;我只想尝试为每个属性注入一个键/值对。

解决方案

不确定这是否完全适用于您的场景(你有一个 Map< Integer,String> 但最后你说你只需要一个Map中的键值对,但也许它可以给你更多想法:




  • 假设一个 @Configuration 类,其中.properties文件被加载为a java.util.Properties 对象:





  @Configuration 
public class Config {

@Bean(name =mapper)
public PropertiesFactoryBean mapper(){
PropertiesFactoryBean bean = new PropertiesFactoryBean();
bean.setLocation(new ClassPathResource(META-INF / spring / application.properties));
返回bean;
}

@Bean
public MyBean myBean(){
return new MyBean();
}
}




  • MyBean 类正在使用这些属性,使用SPeL注入:





 公共类MyBean {

@Value(#{mapper})
私人地图道具;

公共地图getProps(){
返回道具;
}
}

所以,最后你不要使用xml (当然),您需要使用 PropertiesFactoryBean 来加载.properties文件,并使用 @Value ,Spring将属性注入Map。额外的代码(可能是 @PropertySource )是 PropertiesFactoryBean ,你不需要解析代码中的值(与注入 String [] 的解决方法相比)。



希望这会有所帮助。


How does one populate a map of values using the @Values annotation, without defining anything in applicationContext.xml or any other XML file.

I am using spring boot, which doesn't have any XML files, and nor do I want any XML files, so please don't tell me to declare any property reader beans in XML etc.

Also, this is a properties injection question - please don't suggest using a database to store the data - that's not an answer, and not possible for my situation anyway.

Also, I can't use YAML either (due to deployment/operational requirements).

I have tried declaring this injection:

@Value("${myprop}")
Map<Integer, String> map;

And this one

@Value("${myprop.*}")
Map<Integer, String> map;

with these entries application.properties:

myprop.1=One
myprop.2=Two
myprop.3=Three

and then tried

myprop[1]=One
myprop[2]=Two
myprop[3]=Three

But no good - just explodes with

Could not autowire field: ... Could not resolve placeholder 'myprop'

I have found a work-around with an injected String[] specified as key1:value1,key2:value2,... that I then parse in code, but I'd prefer to not do that because a) it's more code, and b) the list is going to be quite long, and all pairs on one line is going to be hard to read and maintain.

Is there a way to automatically build a map from several properties?

I don't care what the property names are, what the field type or the annotation is; I'm just trying to inject one key/value pair per property.

解决方案

Not sure if this applies to your scenario entirely (you have there a Map<Integer, String> but in the end you say you just need a key-value pair in a Map), but maybe it could give you some more ideas:

  • assuming a @Configuration class where the .properties file is loaded as a java.util.Properties object:

@Configuration
public class Config {

    @Bean(name = "mapper")
    public PropertiesFactoryBean mapper() {
        PropertiesFactoryBean bean = new PropertiesFactoryBean();
        bean.setLocation(new ClassPathResource("META-INF/spring/application.properties"));
        return bean;
    }

    @Bean
    public MyBean myBean() {
        return new MyBean();
    }
}

  • the MyBean class where those Properties are being used, injected using SPeL:

public class MyBean {

    @Value("#{mapper}")
    private Map props;

    public Map getProps() {
        return props;
    }
}

So, in the end you don't use xml (of course), you need to use a PropertiesFactoryBean to load the .properties file and, using @Value, Spring will inject the Properties into a Map. The extra code (compared to, probably, @PropertySource) is the PropertiesFactoryBean and you don't need to parse the values in your code manually (compared to your workaround that injects a String[]).

Hope this helps.

这篇关于仅通过@Value注入属性映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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