Java Spring映射合并所有值
我有两个通过Java Spring映射合并所有值,java,spring,Java,Spring,我有两个通过定义的地图(一个是要使用的,另一个是供其他人使用的参考地图)。我想将一个映射的值引用(导入/合并)到另一个ie中 CommonMap -> "someKey" : "someValue" -> "someOtherKey" : "someOtherValue" Map1 -> grab all key value pairs in 'CommonMap' 我尝试了一个简单的例子,定义公共映射并尝试引用它进行属性注入: 编辑#2由
定义的地图(一个是要使用的,另一个是供其他人使用的参考地图)。我想将一个映射的值引用(导入/合并)到另一个ie中
CommonMap
-> "someKey" : "someValue"
-> "someOtherKey" : "someOtherValue"
Map1
-> grab all key value pairs in 'CommonMap'
我尝试了一个简单的例子,定义公共映射并尝试引用它进行属性注入:
编辑#2由于下面的答案,最初的问题得以解决,现在是主要问题
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/util
http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="mainBean" class="SpringTest.SpringTest">
<property name="myBean" ref="myFirstBean"/>
</bean>
<bean id="myFirstBean" class="SpringTest.MyBean">
<constructor-arg value="Hello!"/>
<property name="myMap" ref="myLookup" />
</bean>
<util:map id="myLookup">
<entry key="myKey" value="142"/>
<entry key="myOtherKey" value="7"/>
</util:map>
但是你会得到这个错误
Exception in thread "main" org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException: Line 25 in XML document from class path resource [configuration-spring.xml] is invalid; nested exception is org.xml.sax.SAXParseException; lineNumber: 25; columnNumber: 40; cvc-complex-type.3.2.2: Attribute 'merge' is not allowed to appear in element 'util:map'.
确保在应用程序上下文文件的
bean
标记中完全定义了util
命名空间声明
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
根据:
xmlns:utilxmlns:util=”“的xml条目可以发布吗?看看这个答案谢谢,现在我如何将这些值合并到第二个映射中?您现在遇到了什么问题?
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd">
<bean id="myFirstBean" class="SpringTest.MyBean">
<constructor-arg value="Hello!" />
<property name="myMap" ref="myLookup" />
</bean>
<bean id="commonLookup"
class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map>
<entry key="myKey" value="142" />
<entry key="myOtherKey" value="7" />
</map>
</property>
</bean>
<bean id="myLookup" parent="commonLookup"
class="org.springframework.beans.factory.config.MapFactoryBean">
<property name="sourceMap">
<map merge="true">
<entry key="myLookupKey" value="12" />
</map>
</property>
</bean>