How to load a springframework ApplicationContext from Jython
Posted
by staticman
on Stack Overflow
See other posts from Stack Overflow
or by staticman
Published on 2010-05-12T19:10:50Z
Indexed on
2010/05/12
19:14 UTC
Read the original article
Hit count: 326
I have a class that loads a springframework application context like so:
package com.offlinesupport;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class OfflineScriptSupport {
private static ApplicationContext appCtx;
public static final void initialize() {
appCtx = new ClassPathXmlApplicationContext( new String[] {
"mycontext.spring.xml"
} );
}
public static final ApplicationContext getApplicationContext() {
return appCtx;
}
public static final void main( String[] args ) {
System.out.println( "Starting..." );
initialize();
System.out.println( "loaded" );
}
}
The class OfflineScriptSupport, and the file mycontext.spring.xml are each deployed into separate jars (along with other classes and resources in their respective modules). Lets say the jar files are OfflineScriptSupport.jar and *MyContext.jar". mycontext.spring.xml is put at the root of the jar.
In a Jython script (*myscript.jy"), I try to call the initialize method to create the application context:
from com.offlinesupport import OfflineScriptSupport
OfflineScriptSupport.initialize();
I execute the Jython script with the following command (from Linux):
jython -Dpython.path=spring.jar:OfflineScriptSupport.jar:MyContext.jar myscript.jy
The Springframework application context cannot find the mycontext.spring.xml file. It displays the following error:
java.io.FileNotFoundException: class path resource [mycontext.spring.xml] cannot be opened because it does not exist
at org.springframework.core.io.ClassPathResource.getInputStream(ClassPathResource.java:137)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:167)
at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(XmlBeanDefinitionReader.java:148)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:126)
at org.springframework.beans.factory.support.AbstractBeanDefinitionReader.loadBeanDefinitions(AbstractBeanDefinitionReader.java:142)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:113)
at org.springframework.context.support.AbstractXmlApplicationContext.loadBeanDefinitions(AbstractXmlApplicationContext.java:81)
at org.springframework.context.support.AbstractRefreshableApplicationContext.refreshBeanFactory(AbstractRefreshableApplicationContext.java:89)
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:269)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:87)
at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:72)
at com.offlinesupport.OfflineScriptSupport.initialize(OfflineScriptSupport.java:27)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
If I run the jar directly from Java (using the main entry point in OfflineScriptSupport) it works and there is no error thrown.
Is there something special about the way Jython handles classpaths making the Springframework's ClassPathXmlApplicationContext not work (i.e. not be able to find resource files in the classpath)?
© Stack Overflow or respective owner