Results 1 to 7 of 7

Thread: Can Spring instantiate a static nested class?

  1. #1
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Question Can Spring instantiate a static nested class?

    I'm trying to instantiate a static nested class using a ClassPathXmlApplicationContext, but I get a ClassNotFoundException. Here's my config file:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
      "http://www.springframework.org/dtd/spring-beans.dtd">
    <beans>
      <bean id="inner" class="test.Main.Inner"/>
    </beans>
    And here's my Java code:

    Code:
    package test;
    
    import org.springframework.context.support.ClassPathXmlApplicationContext;
    
    public class Main {
      
      public static void main(String[] args) {
        new ClassPathXmlApplicationContext("test/my-config.xml");
      }
      
      public static class Inner {}
    }
    And here's the error:

    Code:
    17/07/2007 15:34:11 org.springframework.context.support.AbstractApplicationContext refresh
    INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@cdedfd: display name [org.springframework.context.support.ClassPathXmlApplicationContext@cdedfd]; startup date [Tue Jul 17 15:34:11 EST 2007]; root of context hierarchy
    17/07/2007 15:34:11 org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
    INFO: Loading XML bean definitions from class path resource [test/my-config.xml]
    17/07/2007 15:34:12 org.springframework.context.support.AbstractApplicationContext refresh
    INFO: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@cdedfd]: org.springframework.beans.factory.support.DefaultListableBeanFactory@1bc82e7
    17/07/2007 15:34:12 org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
    INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1bc82e7: defining beans [inner]; root of factory hierarchy
    17/07/2007 15:34:12 org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons
    INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1bc82e7: defining beans [inner]; root of factory hierarchy
    Exception in thread "main" org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [test.Main.Inner] for bean with name 'inner' defined in class path resource [test/my-config.xml]; nested exception is java.lang.ClassNotFoundException: test.Main.Inner
    Caused by: java.lang.ClassNotFoundException: test.Main.Inner
    	at java.net.URLClassLoader$1.run(URLClassLoader.java:200)
    	at java.security.AccessController.doPrivileged(Native Method)
    	at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:268)
    	at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    	at org.springframework.util.ClassUtils.forName(ClassUtils.java:201)
    	at org.springframework.beans.factory.support.AbstractBeanDefinition.resolveBeanClass(AbstractBeanDefinition.java:325)
    	at org.springframework.beans.factory.support.AbstractBeanFactory.resolveBeanClass(AbstractBeanFactory.java:1033)
    	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:274)
    	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:360)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:91)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:75)
    	at org.springframework.context.support.ClassPathXmlApplicationContext.<init>(ClassPathXmlApplicationContext.java:65)
    	at test.Main.main(Main.java:19)
    Is this a bug or a feature?

    I'm using Spring 2.0.4, FWIW.

    Thanks for any help.
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  2. #2
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Try "test.Main$Inner".

    Regards,
    Andreas

  3. #3
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Smile Yay!

    Aha! That worked - thank you very much.

    Is this in the Spring docs or did you just know it?
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  4. #4
    Join Date
    Aug 2004
    Posts
    2,715

    Default

    Quote Originally Posted by andrews View Post
    Aha! That worked - thank you very much.
    You're welcome.

    Quote Originally Posted by andrews View Post
    Is this in the Spring docs or did you just know it?
    It is the standard java naming. If you compile your class you will notice that you will have a Main.class and a Main$Inner.class. I think I also read about that naming strategy somewhere but regrettably I cannot rmember where.
    Anyway, glad it works for you.

    Regards,
    Andreas

  5. #5
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    667

    Post Inconsistent class naming

    Quote Originally Posted by Andreas Senft View Post
    It is the standard java naming.
    Looking into this further, it seems you have to refer to the class as test.Main$Inner if you call Class.forName(String), which I guess is what the Spring XML bean factory does. However from within Java code, the class would be referred to as test.Main.Inner (this is also what you get in Eclipse if you right-click on the class and choose "Copy Qualified Name").

    Anyway, thanks again for the learning experience.
    Andrew Swan
    "Now is the EJB of our discontent made glorious Spring"

  6. #6
    Join Date
    Oct 2004
    Location
    Fareham, England
    Posts
    313

    Default

    Quote Originally Posted by andrews View Post
    Is this in the Spring docs or did you just know it?
    It is in the Spring docs, mentioned here in the sidebar entitled 'Inner class names'.

    Cheers
    Rick

  7. #7
    Join Date
    Nov 2005
    Location
    Reutlingen, Germany
    Posts
    2,098

    Default

    Quote Originally Posted by Rick Evans View Post
    It is in the Spring docs, mentioned here in the sidebar entitled 'Inner class names'.
    Feels good, Rick, doesn't it?

    Jörg

Posting Permissions

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