tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Spring > Core > Inner Classes

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.

File Name  :  
/SpringCore001/conf/gen/inner_classes.xml 

File Name  :  
com/bethecoder/tutorials/spring3/basic/TopLevelClass.java 
   
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;
    }
  }
}
   

File Name  :  
com/bethecoder/tutorials/spring3/tests/gen/InnerClasses.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
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 = (TopLevelClassfactory.getBean("topLevelCls");
    System.out.println(topLevelClsObj.getName());
    
    TopLevelClass.NestedClass nestedClsObj = 
      (TopLevelClass.NestedClassfactory.getBean("nestedCls");
    System.out.println(nestedClsObj.getName());
    
    TopLevelClass.NestedClass nestedClsObj2 = 
      (TopLevelClass.NestedClassfactory.getBean("nestedCls2");
    System.out.println(nestedClsObj2.getName());
    
  }

}
   

It gives the following output,
TopLevelClass
NestedClass
NestedClass



 
  


  
bl  br