Sunday, December 9, 2007

Spring - Create bean of type Class

I had a need recently to create a bean using Spring of type Class (java.lang.Class). I also wanted to be able to specify the actual class. So for instance I wanted a bean called "productClass" and wanted to pass in my class name "com.example.product.Product" and let Spring instantiate the bean for me. To accomplish this, I created the bean in the context xml file using the following:

  1. <bean id="productClass" class="java.lang.Class"  
  2. factory-method="forName">  
  3. <constructor-arg value="com.example.product.Product"/>  
  4. </bean>  


This worked great for my purposes, I received my com.example.product.Product Class object.

No comments: