Announcement Announcement Module
Collapse
No announcement yet.
Can Spring instantiate a static nested class? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • 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.

  • #2
    Try "test.Main$Inner".

    Regards,
    Andreas

    Comment


    • #3
      Yay!

      Aha! That worked - thank you very much.

      Is this in the Spring docs or did you just know it?

      Comment


      • #4
        Originally posted by andrews View Post
        Aha! That worked - thank you very much.
        You're welcome.

        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

        Comment


        • #5
          Inconsistent class naming

          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.

          Comment


          • #6
            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

            Comment


            • #7
              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

              Comment

              Working...
              X