PDA

View Full Version : IOC with a Map for Resource Management



John Borys
Aug 27th, 2004, 11:23 AM
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.

Colin Sampaleanu
Aug 27th, 2004, 11:44 AM
Can you define a bit better what you want above and beyond the normal capabilities of the 'map' element supported by the beanfactory/appcontext?

John Borys
Aug 27th, 2004, 12:02 PM
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:



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;

irbouho
Aug 27th, 2004, 02:24 PM
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


<bean id="values"
class="org.springframework.beans.factory.config.MethodInv okingFactoryBean" singleton="true">
<property name="targetObject">
<ref bean="myRefDataService"/>
</property>
<property name="targetMethod">
<value>getRefDataMap</value>
</property>
</bean>

HTH