Results 1 to 4 of 4

Thread: IOC with a Map for Resource Management

  1. #1
    Join Date
    Aug 2004
    Posts
    6

    Default IOC with a Map for Resource Management

    Are there any real world examples (I have seen the documentation on this topic) that show a step by step approach to a newbie that wants to use injection with a Map using the Application context?

    What I am trying to do is have the Application Context populate a Map that will be used to populate all drop downs in the presentation layer. The data is realtively static and I am thinking of it as a resource.

    This post is related to another I made in the web forum on Refence Data Storage/Access. Although it was recommended that I use hibernate, we are thinking that we want a separation of concerns so that this will be treated as more of a service that might be placed on a different server than the Presentation.

  2. #2
    Join Date
    Aug 2004
    Location
    Toronto, Canada
    Posts
    736

    Default

    Can you define a bit better what you want above and beyond the normal capabilities of the 'map' element supported by the beanfactory/appcontext?
    Colin Sampaleanu
    SpringSource - http://www.springsource.com

  3. #3
    Join Date
    Aug 2004
    Posts
    6

    Default

    I want a service to populate the Map. I just want to access the map from the Application context.

    I was thinking of calling the service from a constructor and populating a property Map keyValueMap:

    Code:
    public class RefData {
    	private ArrayList RefDatalist;
    	private Map keyValueMap;
    	public static Logger logger = Logger.getLogger(RefData.class);
    	
    	public RefData()	{
    		RefDataService service = new RefDataService();
    		try {
    			keyValueMap = service.getRefDataMap();
    		}
    		catch (Throwable th) {
    			logger.info("An Exception has been thrown.");
    			logger.info(th.getMessage());
    			throw new ApplicationRuntimeException	(th);			
    		}
    	}
    }
    
    I then want to store this map, built in the constructor, in the Application Context for the presentation layer to access.
    
    The service builds the reference data dynamically so I am not sure that the Map element will work but maybe just constructor injection:
    
    Here is how I build the Map:
    
    public class GetRefDataService extends HibernateDaoSupport {
    	private String key;
    	private PersistSession persist;
    
    	/**
    	 * 
    	 */
    	public GetRefDataService(PersistSession persist) {
    		setPersist(persist);
    	}
    
    	public Map buildKeyValueMap() throws Throwable	{
    		RefData refData = new RefData();
    		Map keyValueMap = new HashMap();	
    
    		ArrayList keys = refData.returnCodeTypeList();
    
    		for &#40;int i=0; i<keys.size&#40;&#41;;i++&#41;	&#123;
    			String key = keys.get&#40;i&#41;.toString&#40;&#41;;
    			Iterator value = getTextForCode&#40;key&#41;;
    			keyValueMap.put&#40;key, value&#41;;
    			
    		&#125;
    		return keyValueMap;
    	&#125;
    	
    	public Map execute&#40;&#41; throws Throwable &#123;		
    		Map refDataMap = buildKeyValueMap&#40;&#41;;
    		return refDataMap;		
    	&#125;
    	
    	private Iterator getTextForCode&#40;String key&#41; throws Throwable	&#123;
    		List list = null;
    		
    		String refDataType = key;
    				
    //Replace "find" with findByNamedQuery here!**************************************************************
    		list = persist.find&#40;"from REF_DATA in class ReferenceData where REF_DATA_TYPE = '"+ refDataType + "'"&#41;;		
    		
    		return list.iterator&#40;&#41;;
    &#125;
    	
    //Accessors here	
    
    &#125;

  4. #4
    Join Date
    Aug 2004
    Location
    Montréal, Canada
    Posts
    845

    Default

    John,
    I do not understand what you are trying to achieve here but if you need to expose a result of a bean method call (that is of type Map) in the appcontext, you can do the following
    Code:
      <bean id="values"
                class="org.springframework.beans.factory.config.MethodInvokingFactoryBean" singleton="true">
        <property name="targetObject">
          <ref bean="myRefDataService"/>
        </property>
        <property name="targetMethod">
         <value>getRefDataMap</value>
        </property>
      </bean>
    HTH
    Omar Irbouh

    Spring Modules Team
    http://irbouh.blogspot.com/

Similar Threads

  1. Replies: 1
    Last Post: Sep 20th, 2005, 09:14 PM
  2. Replies: 38
    Last Post: May 11th, 2005, 02:49 PM
  3. Declarative transaction management
    By trondgzi in forum Data
    Replies: 5
    Last Post: Apr 27th, 2005, 01:05 AM
  4. Transaction management at service layer
    By appuchan in forum Data
    Replies: 1
    Last Post: Mar 30th, 2005, 07:52 AM
  5. Session Management in Spring?
    By spring04 in forum Web
    Replies: 3
    Last Post: Dec 6th, 2004, 02:48 PM

Posting Permissions

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