java 具有动态键和值的 Spring bean 映射

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14689797/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 17:04:10  来源:igfitidea点击:

Spring bean map with dynamic keys and values

javaspringhashmapspring-ioc

提问by Javee

Right now i've application related data in spring bean map and passed this map to other classes as ref.

现在我在 spring bean 映射中拥有与应用程序相关的数据,并将此映射作为 ref 传递给其他类。

Map is defined as below

地图定义如下

<bean id="sampleMap" class="java.util.HashMap">
    <constructor-arg index="0" type="java.util.Map">
        <map key-type="java.lang.Integer" value-type="java.lang.Float">
            <entry key="1" value="5"/>
            <entry key="2" value="10"/>
            <entry key="3" value="15"/>             
        </map>
    </constructor-arg>
</bean>

This map is referred in other beans as

此映射在其他 bean 中称为

<bean id="config" class="com.example.Config" abstract="false">      
    <property name="sampleMap" ref="sampleMap"/>
    .
    .
</bean>

I just want to retrieve the map values from database table and want to inject that to other classes. How can i do that in spring. Basically that table contains application related data.The key and values of map will be int,Configuration.

我只想从数据库表中检索地图值,并将其注入其他类。我怎么能在春天做到这一点。基本上该表包含与应用程序相关的数据。映射的键和值将是 int、Configuration。

What is the best place to load the configuration data? can we do with spring beans map or is there any other good approach to load configuration from database and refer that in other places of application like service,delegate and DAO?

加载配置数据的最佳位置是什么?我们可以使用 spring beans map 还是有其他好的方法可以从数据库加载配置并在其他应用程序位置(如服务、委托和 DAO)中引用它?

Help will be appreciated

帮助将不胜感激

Thanks

谢谢

回答by kan

You should use spring's FactoryBean.

您应该使用 spring 的FactoryBean.

public class MyHashMapFactoryBean implements FactoryBean<Map<Integer, Float>>{

  @Autowired
  private Datasource datasource; 

  public Map<Integer, Float> getObject(){
    return <load from database>;
  }

  public Class<Map> getObjectType() { return Map.class ; }

  public boolean isSingleton() { return true; }
}

<bean id="sampleMap" class="my.package.MyHashMapFactoryBean">
  <here you could pass some properties e.g. jdbc datasource>
</bean>

回答by Sean Patrick Floyd

The FactoryBean answeris correct, but it's the old way of doing things.

FactoryBean的答案是正确的,但它是做事的老办法

Instead, use a @Configurationclass

相反,使用一个@Configuration

@Configuration
public class DBConfiguration{

    @Autowired
    private DataSource dataSource;

    @Bean
    public Map<Integer, Float> configMap(){
        // use dataSource to get map values
    }

}

One of the many advantages to this approach is that you can autowire the DataSourceinto the configuration class.

这种方法的众多优点之一是您可以将其自动装配DataSource到配置类中。

BTW, using a map as bean doesn't feel right. I'd create a wrapper object around the map and use that as Spring Bean, but opinions differ on that.

顺便说一句,使用地图作为 bean 感觉不对。我会在地图周围创建一个包装对象并将其用作 Spring Bean,但对此意见不一。

回答by nefo_x

what i actually found out today, is that when you need map of bean names to instances of specific interface, there is no need of @Qualifier's and any sort of FactoryBeancode. Spring would find and inject candidates for you. Little bit of magic, but it seems to work that way.

我今天实际发现的是,当您需要将 bean 名称映射到特定接口的实例时,不需要@Qualifier's 和任何类型的FactoryBean代码。Spring 会为你寻找并注入候选人。有点神奇,但它似乎是这样工作的。