PDA

View Full Version : IOC with a Map for Resource Management


John Borys
Aug 27th, 2004, 12:23 PM
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, 12:44 PM
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, 01: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 (int i=0; i<keys.size();i++) {
String key = keys.get(i).toString();
Iterator value = getTextForCode(key);
keyValueMap.put(key, value);

}
return keyValueMap;
}

public Map execute() throws Throwable {
Map refDataMap = buildKeyValueMap();
return refDataMap;
}

private Iterator getTextForCode(String key) throws Throwable {
List list = null;

String refDataType = key;

//Replace "find" with findByNamedQuery here!********************************************* *****************
list = persist.find("from REF_DATA in class ReferenceData where REF_DATA_TYPE = '"+ refDataType + "'");

return list.iterator();
}

//Accessors here

}

irbouho
Aug 27th, 2004, 03: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