How to load Springframework ApplicationContext from Jython

I have a class that loads a springframework application context like this:

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 OfflineScriptSupport class and the mycontext.spring.xml file are deployed into separate jars (along with other classes and resources in their respective modules). Let's say the jar files are OfflineScriptSupport.jar and * MyContext.jar files. Mycontext.spring.xml is placed in the root of the jar.

In a Jython script (* myscript.jy "), I am trying to call the initialize method to create an application context:

from com.offlinesupport import OfflineScriptSupport

OfflineScriptSupport.initialize();

      

I am running a Jython script with the following command (from Linux):

jython -Dpython.path=spring.jar:OfflineScriptSupport.jar:MyContext.jar myscript.jy

      

Springframework application script cannot find 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 offline entry point) it works and there is no error.

Is there something special about the way Jython handles classpaths that is causing the SpringFramework ClassPa thanks to the mlApplicationContext to fail (i.e. can't find resource files on the classpath)?

+2


a source to share


3 answers


You may need to use the classpath / mycontext.spring.xml resource to specify the full path.



0


a source


Try using FileSystemXmlApplicationContext ("mycontext.spring.xml") instead.



"Simple paths will always be interpreted as relative to the current VM working directory." http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/context/support/FileSystemXmlApplicationContext.html

0


a source


Make sure your classpath is set correctly. Assuming your application context file is in the WEB-INF directory of your web application, do:

cd webapps/<application>/WEB-INF
java -cp 'lib/*:classes:.' org.python.util.jython
>>> from org.springframework.context.support import ClassPathXmlApplicationContext
>>> ctx = ClassPathXmlApplicationContext(['mycontext.spring.xml'])

      

Pay attention to '.' on the classpath, this explicitly specifies the current directory so that any resources can be loaded. Also note that I am quoting the classpath so the shell does not extend it.

This should download the app.

0


a source







All Articles