2008/09/03

Simple tip: Inject the nested class in Spring context

It's a simple tip and is not mentioned in Spring reference docs:

When you want create a bean in Spring which is a nested class of some owner class, use the "$" to separate the owner class name and the nested class name like following example:

<bean class="org.balah.OwnerClass$NestedClass"
.....

Don't use the convention like "org.balah.OwnerClass.NestedClass" that is used in the source code usually. Because the Spring will use Class.forName() similar mechanism to find the class specified in the bean definition. As you use the Class.forName() to load a nested class, always use "$" to let the classloader locate the exact class file from your classpathes. The "." sperated convention just confused the class resolver anf finally throw a ClassNotFoundException.

Well well... why another J2EE blog? I benefited from other people's technical blogs, and guess what, it's a good idea to contribute some of my works too. Hope it's helpful and useful, to all of your folks.