Inner Classes
Spring Inversion of Control (IoC ) also known as
Dependency Injection (DI ) is a process by which
objects define their dependencies with collaborating objects.
The following example shows Spring bean definition for inner classes.
package com.bethecoder.tutorials.spring3.basic;
public class TopLevelClass {
private String name = "TopLevelClass" ;
public String getName () {
return name;
}
public static class NestedClass {
private String name = "NestedClass" ;
public String getName () {
return name;
}
}
}
package com.bethecoder.tutorials.spring3.tests.gen;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import com.bethecoder.tutorials.spring3.basic.TopLevelClass;
public class InnerClasses {
/**
* @param args
*/
public static void main ( String [] args ) {
XmlBeanFactory factory = new XmlBeanFactory (
new ClassPathResource ( "inner_classes.xml" )) ;
TopLevelClass topLevelClsObj = ( TopLevelClass ) factory.getBean ( "topLevelCls" ) ;
System.out.println ( topLevelClsObj.getName ()) ;
TopLevelClass.NestedClass nestedClsObj =
( TopLevelClass.NestedClass ) factory.getBean ( "nestedCls" ) ;
System.out.println ( nestedClsObj.getName ()) ;
TopLevelClass.NestedClass nestedClsObj2 =
( TopLevelClass.NestedClass ) factory.getBean ( "nestedCls2" ) ;
System.out.println ( nestedClsObj2.getName ()) ;
}
}
It gives the following output,
TopLevelClass
NestedClass
NestedClass