Results 1 to 5 of 5

Thread: Class as Map Key

  1. #1
    Join Date
    Oct 2004
    Location
    Washington, DC
    Posts
    20

    Default 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. #2
    Join Date
    Nov 2004
    Location
    Hilversum - The Netherlands
    Posts
    1,054

    Default

    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.

  3. #3
    Join Date
    Oct 2004
    Location
    Washington, DC
    Posts
    20

    Default

    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

  4. #4
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    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

  5. #5
    Join Date
    Aug 2004
    Posts
    1,905

    Default

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

Similar Threads

  1. Order of Bean definitions matters?
    By cfuser in forum Container
    Replies: 2
    Last Post: Oct 21st, 2005, 10:29 AM
  2. Spring container fails with no exception
    By naor in forum Container
    Replies: 9
    Last Post: Oct 1st, 2005, 03:39 PM
  3. EHCaching Hibernate
    By dencamel in forum Data
    Replies: 3
    Last Post: Sep 6th, 2005, 09:03 PM
  4. Replies: 3
    Last Post: Sep 4th, 2005, 11:11 PM
  5. Stack Overflow
    By rayho222 in forum Container
    Replies: 6
    Last Post: May 17th, 2005, 03:42 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •