RTFM as always... and true enough (obviously) there is support for loading those context files contained in a string array... my crappy spring application context utility class ended looking like this:
/*
* Created on Aug 1, 2004
*
* Author Rogelio M. Nocom Jr.
*
*/
package com.mobileindie.spring;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
/**
* @author nox
*
* based on Richard Hightowers article "orm without the container"
*
*/
public class ApplicationContextFactory {
// logger
private static Log log = LogFactory.getLog(ApplicationContextFactory.class);
// path and filename of application context
private static Object contextObject = null;
// make sure we are not loading twice...
private static int count = 0;
public static void init(String _contextObject) {
if (count >0) {
log.error("Can't initialize the application context twice: THIS SHOULD ONLY HAPPEN DURING TESTING");
}
contextObject = _contextObject;
count++;
}
public static void init(String[] _contextObject) {
if (count >0) {
log.error("Can't initialize the application context twice: THIS SHOULD ONLY HAPPEN DURING TESTING");
}
contextObject = _contextObject;
count++;
}
public static ApplicationContext getApplicationContext() {
if (contextObject == null) {
throw new IllegalStateException("Application context not initialized");
}
else if (contextObject instanceof String) {
String contextPath = (String) contextObject;
return new ClassPathXmlApplicationContext(contextPath);
}
else if (contextObject instanceof String[]) {
String[] contextPath = (String[]) contextObject;
return new ClassPathXmlApplicationContext(contextPath);
}
else {
throw new IllegalStateException("Context must be initialized via String or String[]");
}
}
}
No comments:
Post a Comment