简体   繁体   中英

Spring Beans - wiring class as key and value to map

How would one wire key/value pairs into a property like this ?

 private Map<Class<? extends ObjectOne>, Class<? extends ObjectTwo>> classes;

Both ObjectOne and ObjectTwo are abstract if that is of any help.

you can probably define the bean in appcontext as

    <bean id="id" class = "pathtoyourclass">
    <property name="propname">
    <map>
    <entry key-ref="id of class extends objectone's bean" value-ref="id of class extends objecttwo's bean">
    </map>
    </property>
</bean>

As this stackoverflow post has bit confused me it seems that there is nothing to it wiring map like I declared.

             <map>
                <entry key="org.sandbox.ObjectOneExt"
                       value="org.sandbox.ObjectTwoExt" />

                <!-- where ObjectOneExt and ObjectTwoExt are extended from ObjectOne and ObjectTwo respectively -->
            </map>

Spring (at least 3.1) handled this the way I wanted. So all that is required is to specify full class path to an object in key and value. The "key-type="java.lang.Class"" which is demonstrated in post somehow made spring confused but once removed worked like a charm.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM