Results 1 to 4 of 4

Thread: Using a custom classloader in Spring

  1. #1
    Join Date
    Oct 2009
    Posts
    2

    Default Using a custom classloader in Spring

    Hello guys,

    actually I try to implement an Spring wrapper around a Apache MINA based RMI alternative.

    So far it works quiet fine but there is some problem on reloading the application context since the RMI-System is build on some sort of singleton-like class which holds actual connection sessions.
    On reloading these aren't cleaned gracefully so it'll not reconnect but throws an exception cause session isn't opened as it expects.

    My idea was to wrap all depending classes to a custom classloader which can be thrown away and recreated on application context reload.

    The actual problem is that classes are loaded by BeanDefinitionReader to see if classes are available in classpath.

    I know I can instantiate a classloader right before loading Spring but I don't want a special bootstrap thinggy. So is there any other way to inject a custom classloader right before bean configuration is read?

    Is there any event or interface that can be used?
    Another idea was to define my own namespacehandler and bean definition reader to handle the special classloading stuff but it would be the last chance to get it working since I actually want to be handled it the same way with and without the special namespace.

    Hope someone can help me :-)

    Thx
    Noctarius

  2. #2
    Join Date
    Nov 2004
    Posts
    25

    Default

    Could you use something like

    Code:
    GenericApplicationContext context = new GenericApplicationContext();
    context.setClassLoader(classLoader);
    
    XmlBeanDefinitionReader xmlReader = new XmlBeanDefinitionReader(context);
    //comment out below to register beans without loading classes
    //xmlReader.setBeanClassLoader(classLoader);
    xmlReader.loadBeanDefinitions(new ClassPathResource("..."));
    
    context.refresh();
    Phil Zoio
    Impala - simple dynamic modules for Spring
    http://www.impalaframework.org/
    http://impalablog.blogspot.com/

  3. #3
    Join Date
    Oct 2009
    Posts
    2

    Default

    That way it would be possible but I don't really like the idea of using a bootstrapping mechanism different from Springs standard one.

    It's not for me it should be a general integration for SIMON (the RMI-alternative) so it should work with the existing bootstrapping kinds like Wepapp, standalone, ...

    But thx for the idea

    PS: Is there a way to tell Spring not to use the standard BeanClassLoader (Launcher.AppClassLoader for example) but a different one which becomes instantiated before Spring bootstraps itself?

  4. #4
    Join Date
    Nov 2004
    Posts
    25

    Default

    Quote Originally Posted by Noctarius View Post
    That way it would be possible but I don't really like the idea of using a bootstrapping mechanism different from Springs standard one.
    I'd still regard this bootstrapping mechanism as a standard one - just one designed for more complex requirements.

    Quote Originally Posted by Noctarius View Post
    PS: Is there a way to tell Spring not to use the standard BeanClassLoader (Launcher.AppClassLoader for example) but a different one which becomes instantiated before Spring bootstraps itself?
    You could use the thread context class loader.

    Code:
    ClassLoader existingClassLoader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(classLoader);
    try {
          //instantiate app context
    } finally {
          Thread.currentThread().setContextClassLoader(existingClassLoader);
    }
    Phil Zoio
    Impala - simple dynamic modules for Spring
    http://www.impalaframework.org/
    http://impalablog.blogspot.com/

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •