Java 在春天创建哈希图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19070138/
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-08-12 13:51:55  来源:igfitidea点击:

creating hashmap in spring

javaspringdependency-injectionhashmap

提问by user93796

I am creating a bean is spring as follows

我正在创建一个 bean is spring 如下

<bean id="xyz" class ="java.util.HashMap">
  <constructor-arg  value ="${somehashMapPaceholder}"
</bean>

when i run this i get error as follows:

当我运行它时,我得到如下错误:

Exception in thread "main" org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'personToDept' defined in class path resource [spring-configuration/application/appconfig-beans.xml]: Could not resolve matching constructor (hint: specify index/type/name arguments for simple parameters to avoid type ambiguities)
at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:250)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1035)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:939)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:485)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:456)
at org.springframework.beans.factory.support.AbstractBeanFactory.getObject(AbstractBeanFactory.java:294)
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:225)
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:291)
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:193)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:607)
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:925)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:472)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:139)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:83)

I dont want to create hasmp using due to some other reason.

由于其他原因,我不想使用创建 hasmp。

回答by Jayaram

you have to give the index number/name to the constructor args, while giving value for the constructor. i.e like below

您必须为构造函数参数提供索引号/名称,同时为构造函数提供值。即像下面

<bean id="test" class="java.util.HashMap">
    <constructor-arg>
        <map key-type="java.lang.String" value-type="java.lang.String">
            <entry key="Key 1" value="5" />
            <entry key="Key 2" value="6" />
        </map>
    </constructor-arg>
</bean>

回答by Nelda.techspiress

A HashMap singleton can be implemented in the spring application context and shared across the IOC using the following util:map. First include xmlns:util in the schema.

HashMap 单例可以在 spring 应用程序上下文中实现,并使用以下 util:map 在 IOC 之间共享。首先在架构中包含 xmlns:util。

    xmlns:util="http://www.springframework.org/schema/util"

Then define your shared map:

然后定义您的共享地图:

<util:map id="sendCounts"
    key-type="java.Lang.String"
    value-type="com.mycompany.ToFromCount"
    map-class="java.util.HashMap">
</util:map>

Then in your java source code:

然后在你的java源代码中:

    @Resource Map<String, ToFromCount> sendCounts;//needs to be in IOC for spring batch writer to use.

This sets up your hashmap key to be String, with the value being a class you define. If you wanted the value to be some other POJO or class from some other resource, just use that class as the value-type. The class which injects the map will then fill and access the map.

这将您的 hashmap 键设置为 String,值是您定义的类。如果您希望值是来自其他资源的某个其他 POJO 或类,只需使用该类作为值类型。然后注入地图的类将填充并访问地图。

Using Spring STS in Eclipse, you can also set this up using the nice bean editor with your application context.xml file. See image below. Spring Bean Util Editor

在 Eclipse 中使用 Spring STS,您还可以使用带有应用程序 context.xml 文件的 nice bean 编辑器进行设置。见下图。 Spring Bean 实用程序编辑器

Hope this helps.

希望这可以帮助。