How to create a Spring Bean of an Inner class?

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="$InnerType"/>

Look at the below example to understand this clearly.



import org.springframework.context.ApplicationContext;

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");

Spring Configuration File

<beans xmlns=""
	<context:component-scan base-package=""/>
	<bean id="iBean" class="$Inner"/>


Leave a Reply

Your email address will not be published. Required fields are marked *

Pin It on Pinterest

Share This

Share this post with your friends!