加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – Spring:将属性文件注入map

发布时间:2020-12-15 01:46:00 所属栏目:大数据 来源:网络整理
导读:我有一个属性文件如下: transition.s1=s2,s5transition.s2=s4,s1................... 问题:如何将这些属性注入Map ?你能提供一个例子吗? 最佳答案 在XML配置的情况下 public class StateGraph { public StateGraph(Map 当属性实现map时,您可以将其作为构

我有一个属性文件如下:

transition.s1=s2,s5
transition.s2=s4,s1
...................

问题:如何将这些属性注入Map< String,String>?你能提供一个例子吗?

最佳答案
在XML配置的情况下

public class StateGraph {
    public StateGraph(Map

当属性实现map时,您可以将其作为构造函数提供

请注意,Spring将完成所有the required generic type conversions

If you are using Java 5 or Java 6,you will be aware that it is
possible to have strongly typed collections (using generic types).
That is,it is possible to declare a Collection type such that it can
only contain String elements (for example). If you are using Spring to
dependency inject a strongly-typed Collection into a bean,you can
take advantage of Spring’s type-conversion support such that the
elements of your strongly-typed Collection instances will be converted
to the appropriate type prior to being added to the Collection.

如果您使用的是编程配置,那么您必须自己在@Configuration类中执行此操作 – 请参阅Converting java.util.Properties To HashMap

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读