Announcement Announcement Module
Collapse
No announcement yet.
Class as Map Key Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Class as Map Key

    Hello,
    Is it possiable to have a Class object be a key for a map. Currently when I inject it with spring , my key is a String rather than a Class.

    I believe there was a recent fix to allow non string keys, were Classes addressed in this fix also?

    I have stepped throught the code and ClassEditor doesn't seem to get called for the map keys.

    I am using Spring 1.2.4

    Below is snippet from my application context.

    Thanks for any help,
    Tyler


    Code:
    <property name="daoMap">
    			<map>
    				
    				<entry key="org.example.webapp.domain.PhoneBook" value-ref="phoneBookDAO" />
    				<entry key="org.example.webapp.domain.Salary" value-ref="salaryDAO" />
    				<entry key="org.example.webapp.domain.MessageBoard" value-ref="messageBoardDAO" />
    				
    			</map>
    		</property>

  • #2
    Maybe you could use something like this:

    Code:
     public class ClassFactoryBean implements FactoryBean,InitializingBean&#123;
     	
     	public ClassFactoryBean&#40;String className&#41;&#123;
     		_className = className;
     	&#125;
     
     	public ClassFactoryBean&#40;&#41;&#123;
     	&#125;
    
     	public afterPropertiesSet&#40;&#41;&#123;
     		if&#40;_className == null&#41;
     			throw new IllegalArgumentException&#40;"className is missing"&#41;;
     	&#125;
     	
     	public void setClass&#40;String className&#41;&#123;
     		_className = className;
     	&#125;
     	
     	public Object getObject&#40;&#41;&#123;
     		return Class.forName&#40;_className&#41;;
     	&#125;	
     	
     	public Class getObjectType&#40;&#41;&#123;
     		return Class.class;
     	&#125;
     	
     	public boolean isSingleton&#40;&#41;&#123;
     		return true;
     	&#125;
     &#125;
    Code:
    <property name="daoMap">
    	<map>            
    		<entry>
    			<key>
    				<bean class="ClassFactoryBean">
    					<constructor-arg value="org.example.webapp.domain.Salary"/>
    				</bean>
    			</key>
    			<value><ref bean="salaryDao"/><value>
    		</entry>		
    	</map>
    </property
    I must admit it isn`t the most prettiest thing I have seen. But it should work.

    Comment


    • #3
      Yeah I could do that. But I agree its not the prettiest xml in the world.

      Other thing I could do is to change my code that pulls out the DAO from the map by using myObject.getClass().getName() Or Maybe put a a conditional to do both, to cover both my bases. But I not happy with the that solution either.

      Funny thing is I thought I had this working before in some other code I did. I will try and search for it and see if it works the way I remember it.

      I will post what I find.

      Thanks,
      Tyler

      Comment


      • #4
        Hi

        Use the <key/> element as a child element within your <entry/> for your <map/>; to wit...

        Code:
        <property name="daoMap">
        <map>
        	<entry value-ref="phoneBookDAO">
        		<key><value>org.example.webapp.domain.PhoneBook</value></key>
        	</entry>
        </map>
        </property>
        It sure would be nice if the <key/> element supported the shortcut 'value' and 'ref' attributes too though... it doesn't so don't go there

        The fix you mentioned can be found here.

        The DTD is always a good place to start looking for what can and can't be done XML-wise.

        Ciao
        Rick

        Comment


        • #5
          Can you not specify
          Code:
          <value type="java.lang.Class">my.company.MyClass</value>

          Comment

          Working...
          X