In this tutorial I will explain how to declare bean definition for the Inner classes in Spring XML configuration. When you are using inner class as spring beans, you have to specify the binary class name of that inner or nested class to instantiate as a bean. If you have a class ExampleType and it consist of an inner class InnerType. The binary name for the inner class would be ExampleType$InnerType. This has to be specified in the class attribute of the bean element.
<bean id="iBean" class="javabeat.net.ExampleType$InnerType"/>
Look at the below example to understand this clearly.
SpringBeans.java
package javabeat.net; import javabeat.net.SimpleBean.Inner; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringBeans { public static void main (String args[]){ ApplicationContext applicationContext = new ClassPathXmlApplicationContext("file:/home/krishna/git/SpringTestFramework/Spring TestFramework/WebContent/WEB-INF/dispatcher-servlet.xml"); Inner inner = (Inner)applicationContext.getBean("iBean"); inner.print(); } }
Spring Configuration File
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd"> <context:component-scan base-package="javabeat.net"/> <bean id="iBean" class="javabeat.net.SimpleBean$Inner"/> </beans>